Przejdź do głównej zawartości

Jak wstawić znacznik daty do komórki, jeśli zaznaczono pole wyboru w programie Excel?

Zwykle datownik wprowadza się za pomocą skrótów klawiszowych w programie Excel. Co powiesz na wstawienie daty do komórki za pomocą pola wyboru w programie Excel? Po zaznaczeniu tego pola wyboru znacznik czasu jest wstawiany automatycznie do określonej komórki. Ten artykuł pomoże Ci go rozwiązać.

Wstaw znacznik daty do komórki, jeśli zaznaczono pole wyboru z kodem VBA


Wstaw znacznik daty do komórki, jeśli zaznaczono pole wyboru z kodem VBA

W tej sekcji wprowadzimy skrypt VBA, który pomoże Ci automatycznie wstawić znacznik daty do komórki, jeśli zaznaczysz pole wyboru w programie Excel. Wykonaj następujące czynności.

1. Po wstawieniu pola wyboru naciśnij inny + F11 klawisze jednocześnie, aby otworzyć Microsoft Visual Basic for Applications okno.

2. w Microsoft Visual Basic for Applications okno, kliknij wstawka > Moduł. Następnie skopiuj i wklej poniższy kod VBA do okna Module.

Kod VBA: wstaw znacznik daty do komórki, jeśli zaznaczono pole wyboru

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. naciśnij inny + Q klucze do zamknięcia Microsoft Visual Basic for Applications okno.

4. Kliknij prawym przyciskiem pole wyboru i wybierz Przypisz Micro z menu po kliknięciu prawym przyciskiem myszy. Zobacz zrzut ekranu:

5. w Przypisz makro okno dialogowe, wybierz CheckBox_Date_Stamp Nazwa makra a następnie kliknij OK przycisk. Zobacz zrzut ekranu:

Po zaznaczeniu pola wyboru znacznik daty zostanie automatycznie wstawiony do sąsiedniej komórki.


Powiązane artykuły:

Najlepsze narzędzia biurowe

🤖 Pomocnik AI Kutools: Zrewolucjonizuj analizę danych w oparciu o: Inteligentne wykonanie   |  Wygeneruj kod  |  Twórz niestandardowe formuły  |  Analizuj dane i generuj wykresy  |  Wywołaj funkcje Kutools...
Popularne funkcje: Znajdź, wyróżnij lub zidentyfikuj duplikaty   |  Usuń puste wiersze   |  Łącz kolumny lub komórki bez utraty danych   |   Okrągły bez wzoru ...
Super wyszukiwanie: Wiele kryteriów VLookup    Wiele wartości VLookup  |   Przeglądanie pionowe na wielu arkuszach   |   Wyszukiwanie rozmyte ....
Zaawansowana lista rozwijana: Szybko twórz listę rozwijaną   |  Zależna lista rozwijana   |  Lista rozwijana wielokrotnego wyboru ....
Menedżer kolumn: Dodaj określoną liczbę kolumn  |  Przesuń kolumny  |  Przełącz stan widoczności ukrytych kolumn  |  Porównaj zakresy i kolumny ...
Polecane funkcje: Fokus siatki   |  Widok projektu   |   Duży pasek formuły    Menedżer skoroszytów i arkuszy   |  Biblioteka zasobów (Automatyczny tekst)   |  Selektor dat   |  Połącz arkusze   |  Szyfruj/odszyfruj komórki    Wysyłaj e-maile według listy   |  Super filtr   |   Specjalny filtr (filtruj pogrubienie/kursywa/przekreślenie...) ...
15 najlepszych zestawów narzędzi12 Tekst Tools (Dodaj tekst, Usuń znaki, ...)   |   50 + Wykres rodzaje (Wykres Gantta, ...)   |   40+ Praktyczne Wzory (Oblicz wiek na podstawie urodzin, ...)   |   19 Wprowadzenie Tools (Wstaw kod QR, Wstaw obraz ze ścieżki, ...)   |   12 Konwersja Tools (Liczby na słowa, Przeliczanie walut, ...)   |   7 Połącz i podziel Tools (Zaawansowane wiersze łączenia, Podział komórki, ...)   |   ... i więcej

Zwiększ swoje umiejętności Excela dzięki Kutools for Excel i doświadcz wydajności jak nigdy dotąd. Kutools dla programu Excel oferuje ponad 300 zaawansowanych funkcji zwiększających produktywność i oszczędzających czas.  Kliknij tutaj, aby uzyskać funkcję, której najbardziej potrzebujesz...

Opis


Karta Office wprowadza interfejs z zakładkami do pakietu Office i znacznie ułatwia pracę

  • Włącz edycję i czytanie na kartach w programach Word, Excel, PowerPoint, Publisher, Access, Visio i Project.
  • Otwieraj i twórz wiele dokumentów w nowych kartach tego samego okna, a nie w nowych oknach.
  • Zwiększa produktywność o 50% i redukuje setki kliknięć myszką każdego dnia!
Comments (22)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi, how can I loop the code to include all checkboxes in the column ?
This comment was minimized by the moderator on the site
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.

<div data-tag="code">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
This comment was minimized by the moderator on the site
Thank you very much!! Brilliant! Thank you!
This comment was minimized by the moderator on the site
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?
This comment was minimized by the moderator on the site
Hi Steve,
Please replace the fourth line in the code with With xChk.TopLeftCell.Offset(1, 1).
This comment was minimized by the moderator on the site
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!
This comment was minimized by the moderator on the site
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).
This comment was minimized by the moderator on the site
how do i get the date stamp to be displayed under my check box?
This comment was minimized by the moderator on the site
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!
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
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. ?
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
I am also having that exact same issue. "HELP! I need somebody HELP! Not just anybody HELP! You know I need someone HELP!
This comment was minimized by the moderator on the site
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!
This comment was minimized by the moderator on the site
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.
This comment was minimized by the moderator on the site
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.
This comment was minimized by the moderator on the site
Good Day,
Please assign the macro individually to each checkbox.
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
Is there any way to do this with the date AND time? Thanks for the info either way!
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
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)
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations