Note: The other languages of the website are Google-translated. Back to English
Se connecter  \/ 
x
or
x
INSCRIPTION  \/ 
x

or

Comment insérer un horodateur dans une cellule si une case à cocher est cochée dans Excel?

Normalement, vous entrez un horodatage à l'aide des touches de raccourci dans Excel. Que diriez-vous d'insérer un horodatage dans une cellule par une case à cocher dans Excel? Lorsque vous cochez la case, l'horodatage est inséré automatiquement dans une cellule spécifiée. Cet article vous aidera à le résoudre.

Insérez le cachet de la date dans une cellule si vous cochez une case avec le code VBA


Insérez le cachet de la date dans une cellule si vous cochez une case avec le code VBA


Cette section présentera un script VBA pour vous aider à insérer automatiquement un horodateur dans une cellule si vous cochez une case dans Excel. Veuillez faire comme suit.

1. Après avoir inséré une case à cocher, appuyez sur autre + F11 touches simultanément pour ouvrir le Microsoft Visual Basic pour applications fenêtre.

2. dans le Microsoft Visual Basic pour applications fenêtre, cliquez sur insérer > Module. Ensuite, copiez et collez le code VBA ci-dessous dans la fenêtre Module.

Code VBA: insérer un horodatage dans une cellule si une case est cochée

Sub CheckBox_Date_Stamp()
Dim xChk As CheckBox
Set xChk = ActiveSheet.CheckBoxes(Application.Caller)
With xChk.TopLeftCell.Offset(, 1)
    If xChk.Value = xlOff Then
        .Value = ""
    Else
       .Value = Date
    End If
End With
End Sub

3. presse autre + Q touches pour fermer le Microsoft Visual Basic pour applications fenêtre.

4. Cliquez avec le bouton droit sur la case à cocher et sélectionnez Attribuer Micro dans le menu contextuel. Voir la capture d'écran:

5. dans le Attribuer une macro boîte de dialogue, sélectionnez CheckBox_Date_Stamp et Nom de la macro , puis cliquez sur le OK bouton. Voir la capture d'écran:

Lorsque vous cochez la case, le timbre de date sera inséré automatiquement dans la cellule adjacente.


Articles connexes:


Les meilleurs outils de productivité de bureau

Kutools for Excel résout la plupart de vos problèmes et augmente votre productivité de 80%

  • Réutilisation: Insérer rapidement formules complexes, graphiques et tout ce que vous avez utilisé auparavant; Crypter les cellules avec mot de passe; Créer une liste de diffusion et envoyer des e-mails ...
  • Barre Super Formula (modifiez facilement plusieurs lignes de texte et de formule); Disposition de lecture (lire et modifier facilement un grand nombre de cellules); Coller dans la plage filtrée...
  • Fusionner les cellules / lignes / colonnes sans perdre de données; Contenu des cellules divisées; Combiner des lignes / colonnes en double... Empêcher les cellules en double; Comparer les gammes...
  • Sélectionnez Dupliquer ou Unique Lignes; Sélectionnez les lignes vides (toutes les cellules sont vides); Super Find et Fuzzy Find dans de nombreux classeurs; Sélection aléatoire ...
  • Copie exacte Plusieurs cellules sans changer la référence de formule; Créer automatiquement des références à plusieurs feuilles; Insérer des puces, Cases à cocher et plus encore ...
  • Extrait du texte, Ajouter du texte, Supprimer par position, Supprimer l'espace; Créer et imprimer des sous-totaux de pagination; Conversion entre le contenu et les commentaires des cellules...
  • Super filtre (enregistrer et appliquer des schémas de filtrage à d'autres feuilles); Tri avancé par mois / semaine / jour, fréquence et plus; Filtre spécial par gras, italique ...
  • Combiner des classeurs et des feuilles de travail; Fusionner les tableaux en fonction des colonnes clés; Diviser les données en plusieurs feuilles; Conversion par lots xls, xlsx et PDF...
  • Plus de 300 fonctionnalités puissantes. Prend en charge Office / Excel 2007-2019 et 365. Prend en charge toutes les langues. Déploiement facile dans votre entreprise ou organisation. Essai gratuit de 30 jours. Garantie de remboursement de 60 jours.
onglet kte 201905

Office Tab apporte une interface à onglets à Office et simplifie considérablement votre travail

  • Activer l'édition et la lecture par onglets dans Word, Excel, PowerPoint, Publisher, Access, Visio et Project.
  • Ouvrez et créez plusieurs documents dans de nouveaux onglets de la même fenêtre, plutôt que dans de nouvelles fenêtres.
  • Augmente votre productivité de 50% et réduit des centaines de clics de souris chaque jour!
bas de cabine
Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Ahmed Hassan · 9 days ago
    Hi, how can I loop the code to include all checkboxes in the column ?
    • To post as a guest, your comment is unpublished.
      crystal · 6 days ago
      Good day,
      If there are lots of checkboxes in rows and you want to act on all check boxes at the same time, the below VBA code can help you.
      Please copy the codes below into a Module code window, go back to the worksheet and create a button (such as a Button (Form Control)), assign the macro SetAllChkChange() to the button, then click the button to run the code.

      Now all check boxes in rows of your worksheet have been activate. You can check any one of them to insert date stamp in its adjacent cell.

      Sub SetAllChkChange() 'Updated by Extendoffice 20211130 Dim xChks Dim xChk As CheckBox On Error Resume Next Set xChks = ActiveSheet.CheckBoxes For Each xChk In xChks xChk.Select Selection.OnAction = "ObjChkChange" Next End Sub Sub ObjChkChange() Dim xChk As CheckBox Set xChk = ActiveSheet.CheckBoxes(Application.Caller) With xChk.TopLeftCell.Offset(, 1) If xChk.Value = xlOff Then .Value = "" Else .Value = Date End If End With End Sub
  • To post as a guest, your comment is unpublished.
    Hash · 1 years ago
    Thank you very much!! Brilliant! Thank you!
  • To post as a guest, your comment is unpublished.
    steve · 1 years ago
    I used the VBA for the checkbox time stamp, but the first two cells are not working correctly. My first check box is in A2, when I check the box, the time is posted in B1. How do I fix this?
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi Steve,
      Please replace the fourth line in the code with With xChk.TopLeftCell.Offset(1, 1).
  • To post as a guest, your comment is unpublished.
    Cj · 1 years ago
    hi! i can't seem to find a way where in the datestamp will be on the side of the check box. i tried changing the offset value from 0, 1 and -1. can you help me with this? thank you!
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi, supposing your check box is in A2 and want to output the datestamp on the right side of the check box (in this case it is B2), please change the Offset value to Offset(1, 1).
  • To post as a guest, your comment is unpublished.
    Mehpire · 3 years ago
    how do i get the date stamp to be displayed under my check box?
  • To post as a guest, your comment is unpublished.
    Amy H · 3 years ago
    Hi! Thanks for the code. It works perfectly with a lil tweaking on the offset. However, i was working on a sheet which has many many manyyy rows (~500+ rows) which contains load of check boxes and the file size grew significantly. Is there any way to reduce the size? Any alternative way to do this?

    Thanks!
    • To post as a guest, your comment is unpublished.
      crystal · 3 years ago
      Good da
      If there are lots of check boxes in rows and you want to act on all check boxes at the same time, the below VBA code can help you.
      Please copy the codes into a Module code window, go back to the worksheet and create a button (such as a Button (Form Control)), assign the macro SetAllChkChange() to the button, then click the button to run the code.

      Now all check boxes in rows of your worksheet have been activate. You can check any one of them to insert date stamp in its adjacent cell.

      Sub SetAllChkChange()
      Dim xChks
      Dim xChk As CheckBox
      Dim xI As Long
      On Error Resume Next
      Erase xArrChk
      Set xChks = ActiveSheet.CheckBoxes
      ReDim Preserve xArrChk(1 To xChks.count)
      xI = 1
      For Each xChk In xChks
      xChk.Select
      Selection.OnAction = "ObjChkChange"
      Next
      End Sub


      Sub ObjChkChange()
      Dim xChk As CheckBox
      Set xChk = ActiveSheet.CheckBoxes(Application.Caller)
      With xChk.TopLeftCell.Offset(, 1)
      If xChk.Value = xlOff Then
      .Value = ""
      Else
      .Value = Date
      End If
      End With
      End Sub
  • To post as a guest, your comment is unpublished.
    Mallory V · 4 years ago
    I copy & pasted the VBA code exactly, but on my spreadsheet, the date appears in the cell above and to the right of the check box column, not in the cell directly to the right. ?
    • To post as a guest, your comment is unpublished.
      Jacob W · 3 years ago
      This is what I did to fix that issue

      Sub CheckBox_Date_Stamp()
      Dim xChk As CheckBox
      Set xChk = ActiveSheet.CheckBoxes(Application.Caller)
      With xChk.TopLeftCell.Offset(1, 1)
      If xChk.Value = xlOff Then
      .Value = ""
      Else
      .Value = Now()
      End If
      End With
      End Sub
    • To post as a guest, your comment is unpublished.
      CW · 4 years ago
      I am also having that exact same issue. "HELP! I need somebody HELP! Not just anybody HELP! You know I need someone HELP!
      • To post as a guest, your comment is unpublished.
        Kev · 4 years ago
        I had the same issue with the date and time stamp appearing in the cell above the intended cell (using Excel 2007). I went ahead and altered the "offset" formula so that it goes over one cell AND down one cell and now the stamp is appearing where I want it: With xChk.TopLeftCell.Offset(1, 1)
        I am updating a worksheet that someone else created and didn't create the checkboxes, but it may have to do with where inside the cell the check box is placed. I got different results when I lined up the checkbox with the bottom of the cell.

        Hope that helps!
      • To post as a guest, your comment is unpublished.
        crystal · 4 years ago
        Good day,
        The code works well in my case. After checking the check box, the date will appear in the cell directly to the right. Would you provide a screenshot of your case. And which Office version do you use. Thank you.
  • To post as a guest, your comment is unpublished.
    June · 4 years ago
    HI I tried this formula and it worked only for A1 and B1 when I applied the macro to to the checkbox in A1. However, when I applied the macro to the checkbox in A2, nothing happened in B2. Also how would the formula be changed if I wanted to use this for a checklist? If column A was the checkboxes and column C was completed date.
    • To post as a guest, your comment is unpublished.
      crystal · 4 years ago
      Good Day,
      Please assign the macro individually to each checkbox.
    • To post as a guest, your comment is unpublished.
      Miriam Alfy · 4 years ago
      Hi:) you can copy the cell with the checkbox in A1 to the rest of the column. or assign the macro individually to each checkbox
  • To post as a guest, your comment is unpublished.
    Sarah T · 4 years ago
    Is there any way to do this with the date AND time? Thanks for the info either way!
    • To post as a guest, your comment is unpublished.
      crystal · 4 years ago
      Hi,
      Please apply below VBA code to add date and time.

      Sub CheckBox_Date_Stamp()
      Dim xChk As CheckBox
      Set xChk = ActiveSheet.CheckBoxes(Application.Caller)
      With xChk.TopLeftCell.Offset(, 1)
      If xChk.Value = xlOff Then
      .Value = ""
      Else
      .Value = Now()
      End If
      End With
      End Sub
    • To post as a guest, your comment is unpublished.
      Benoit T · 4 years ago
      Sub CheckBox_Date_Stamp()
      Dim xChk As CheckBox
      Set xChk = ActiveSheet.CheckBoxes(Application.Caller)
      With xChk.TopLeftCell.Offset(, 1)
      If xChk.Value = xlOff Then
      .Value = ""
      Else
      .Value = Date & " " & Time
      End If
      End With
      End Sub
  • To post as a guest, your comment is unpublished.
    Ines · 4 years ago
    Thank you, this was very helpful, I would like to note that I find it more useful to have the date stamp to the left of the check box. to do this you just change the offset to (, -1)