Přejít k hlavnímu obsahu

Jak automaticky zaznamenat datum a čas při změně buňky?

Je pro nás snadné ručně vložit statické datum a čas nebo vložit dynamické datum měnící se systémovým časem pomocí vzorce. Pokud chcete automaticky zaznamenat datum a čas při změně nebo zadání hodnot, může být tento problém poněkud odlišný. V tomto článku však můžete tento úkol vyřešit pomocí následujících kroků.

Automatické zaznamenávání data a času při změně buňky pomocí kódu VBA


šipka modrá pravá bublina Automatické zaznamenávání data a času při změně buňky pomocí kódu VBA

Například mám řadu hodnot a teď, když změním nebo zadám nové hodnoty ve sloupci B, chci tam automaticky zaznamenat aktuální datum a čas ve sloupci C, jak ukazuje následující snímek obrazovky:

doc-update-time-value-changes-1

Tuto úlohu můžete dokončit pomocí následujícího kódu VBA. Udělejte prosím toto:

1. Podržte ALT + F11 klávesy pro otevření Okno Microsoft Visual Basic pro aplikace.

2. Poté vyberte použitý list zleva Průzkumník projektupoklepáním otevřete soubor Modula poté zkopírujte a vložte následující kód VBA do prázdného modulu:

Kód VBA: Při změně buňky se automaticky zaznamenává datum a čas

Private Sub Worksheet_Change(ByVal Target As Range)
'Update 20140722
Dim WorkRng As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("B:B"), Target)
xOffsetColumn = 1
If Not WorkRng Is Nothing Then
    Application.EnableEvents = False
    For Each Rng In WorkRng
        If Not VBA.IsEmpty(Rng.Value) Then
            Rng.Offset(0, xOffsetColumn).Value = Now
            Rng.Offset(0, xOffsetColumn).NumberFormat = "dd-mm-yyyy, hh:mm:ss"
        Else
            Rng.Offset(0, xOffsetColumn).ClearContents
        End If
    Next
    Application.EnableEvents = True
End If
End Sub

doc-update-time-value-changes-1

3. Poté tento kód uložte a zavřete, abyste se vrátili do listu, nyní, když změníte hodnotu buňky nebo zadáte nová data do sloupce B, datum a čas se automaticky zaznamená do sloupce C.

Poznámky:

1. Ve výše uvedeném kódu můžete upravit „B: B„Do kteréhokoli jiného sloupce, u kterého chcete změnit hodnoty buněk v tomto skriptu: Nastavit WorkRng = Intersect (Application.ActiveSheet.Range ("B: B"), Target).

2. S tím xOffsetColumn = 1 skript, můžete vložit a aktualizovat datum a čas do prvního sloupce vedle sloupce se změnou hodnoty, můžete změnit číslo 1 na jiná čísla, například 2,3,4,5… to znamená, že do data bude vloženo druhý, třetí, čtvrtý nebo pátý sloupec kromě sloupce změněných hodnot.

3. Když odstraníte hodnotu ve změněném sloupci, bude odstraněno také datum a čas.

Nejlepší nástroje pro produktivitu v kanceláři

🤖 Kutools AI asistent: Revoluční analýza dat založená na: Inteligentní provedení   |  Generovat kód  |  Vytvořte vlastní vzorce  |  Analyzujte data a generujte grafy  |  Vyvolejte funkce Kutools...
Populární funkce: Najít, zvýraznit nebo identifikovat duplikáty   |  Odstranit prázdné řádky   |  Kombinujte sloupce nebo buňky bez ztráty dat   |   Kolo bez vzorce ...
Super vyhledávání: Více kritérií VLookup    VLookup s více hodnotami  |   VLookup na více listech   |   Fuzzy vyhledávání ....
Pokročilý rozevírací seznam: Rychle vytvořte rozevírací seznam   |  Závislý rozbalovací seznam   |  Vícenásobný výběr rozevíracího seznamu ....
Správce sloupců: Přidejte konkrétní počet sloupců  |  Přesunout sloupce  |  Přepnout stav viditelnosti skrytých sloupců  |  Porovnejte rozsahy a sloupce ...
Doporučené funkce: Zaměření mřížky   |  Návrhové zobrazení   |   Velký Formula Bar    Správce sešitů a listů   |  Knihovna zdrojů (Automatický text)   |  Výběr data   |  Zkombinujte pracovní listy   |  Šifrovat/dešifrovat buňky    Odesílat e-maily podle seznamu   |  Super filtr   |   Speciální filtr (filtr tučné/kurzíva/přeškrtnuté...) ...
Top 15 sad nástrojů12 Text Tools (doplnit text, Odebrat znaky, ...)   |   50+ Graf Typ nemovitosti (Ganttův diagram, ...)   |   40+ Praktické Vzorce (Vypočítejte věk na základě narozenin, ...)   |   19 Vložení Tools (Vložte QR kód, Vložit obrázek z cesty, ...)   |   12 Konverze Tools (Čísla na slova, Přepočet měny, ...)   |   7 Sloučit a rozdělit Tools (Pokročilé kombinování řádků, Rozdělit buňky, ...)   |   ... a více

Rozšiřte své dovednosti Excel pomocí Kutools pro Excel a zažijte efektivitu jako nikdy předtím. Kutools for Excel nabízí více než 300 pokročilých funkcí pro zvýšení produktivity a úsporu času.  Kliknutím sem získáte funkci, kterou nejvíce potřebujete...

Popis


Office Tab přináší do Office rozhraní s kartami a usnadňuje vám práci

  • Povolte úpravy a čtení na kartách ve Wordu, Excelu, PowerPointu, Publisher, Access, Visio a Project.
  • Otevřete a vytvořte více dokumentů na nových kartách ve stejném okně, nikoli v nových oknech.
  • Zvyšuje vaši produktivitu o 50%a snižuje stovky kliknutí myší každý den!
Comments (109)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi,
Is it is also possible to have a VBA code that is able to create a timestamp for cells that have a formula in it? So when the value changes (changes from empty cell to a cell with a value >0 in it) the timestamp appears?
Kind regards,
Femke
This comment was minimized by the moderator on the site
Hello, Femke,
To solve your problem, please apply the below VBA code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Updateby ExtendOffice
Dim WorkRng As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
Dim xDRg As Range

On Error Resume Next
Set xDRg = Target.DirectDependents

If Not xDRg Is Nothing Then

Set WorkRng = Intersect(Application.ActiveSheet.Range("B:B"), xDRg)

xOffsetColumn = 1
If Not WorkRng Is Nothing Then
    Application.EnableEvents = False
    For Each Rng In WorkRng
        If Not VBA.IsEmpty(Rng.Value) Then
            Rng.Offset(0, xOffsetColumn).Value = Now
            Rng.Offset(0, xOffsetColumn).NumberFormat = "dd-mm-yyyy, hh:mm:ss"
        Else
            Rng.Offset(0, xOffsetColumn).ClearContents
        End If
    Next
    Application.EnableEvents = True
End If
Else
Set WorkRng = Intersect(Application.ActiveSheet.Range("B:B"), Target)
xOffsetColumn = 1
If Not WorkRng Is Nothing Then
    Application.EnableEvents = False
    For Each Rng In WorkRng
        If Not VBA.IsEmpty(Rng.Value) Then
            Rng.Offset(0, xOffsetColumn).Value = Now
            Rng.Offset(0, xOffsetColumn).NumberFormat = "dd-mm-yyyy, hh:mm:ss"
        Else
            Rng.Offset(0, xOffsetColumn).ClearContents
        End If
    Next
    Application.EnableEvents = True
End If

End If
End Sub


Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
Sir ! I have a problem that I have data entering Column B in sheet 1 and Date Column C in sheet 2....please guide me what will be the code for that...Anyone
This comment was minimized by the moderator on the site
I want to use this code to update multiple cells within the same sheet. How do I write this?For example, when cell C12 changes, C13 gets the datewhen cell C26 changes, C27 gets the date
when cell G13 changes, G14 gets the date
and so on
This comment was minimized by the moderator on the site
datetime    auto_cycle    tool    time_difference
01-01-2021 :10:10:09    1    1    00:00:11
03-01-2021 :10:10:20    1    2    00:00:02
13-10-2021 :10:10:22    1    3    00:00:04
13-10-2021 :10:10:26    1    4    00:00:04
13-10-2021 :10:10:30    1    5    00:00:06
13-10-2021 :10:10:36    1    6    00:00:02
13-10-2021 :10:10:38    1    7    00:00:05
13-10-2021 :10:10:43    1    8    00:00:00
13-10-2021 :10:10:43    1    9    00:00:06
13-10-2021 :10:10:49    1    10    00:00:03
13-10-2021 :10:10:52    1    11    00:00:08
13-10-2021 :10:11:00    1    13    00:00:10
13-10-2021 :10:11:10    1    12    00:00:04
13-10-2021 :10:11:14    1    14    00:00:05
13-10-2021 :10:11:19    1    16    00:00:04
13-10-2021 :10:11:23    1    17    00:00:04
13-10-2021 :10:11:27    1    18    00:00:02
13-10-2021 :10:11:29    1    19    00:00:04
13-10-2021 :10:11:33    1    20    00:00:05
13-10-2021 :10:11:38    1    21    00:00:07
13-10-2021 :10:11:45    1    12    

this is my master file suppose i change the time value in a column corresponding time difference sshould be update in d column automatically without trigger the macro assigning button could you please help me out 
This comment was minimized by the moderator on the site
I see a couple of people asking about formulas and I'm sorry if I missed the answer somewhere in the thread. I am updating very large data sets that then are migrated to hidden sheets. I have those sheets each set with this "latest update" code but since everything on those sheets references back to the larger data set, it does not see a cell update as the formulas remain the same even thought the values change. I need it to update when the value changes and not jus when the content of the cell changes.
This comment was minimized by the moderator on the site
Sir ! Did you find solution of your problem then please share it with me.I am also facing the same issue...I am new in this...Please
This comment was minimized by the moderator on the site
Private Sub Worksheet_Change(ByVal Target As Range)
'Update 20140722
Dim WorkRng As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("A:A"), Target)
xOffsetColumn = 1
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each Rng In WorkRng
If Not VBA.IsEmpty(Rng.Value) Then
Rng.Offset(0, xOffsetColumn).Value = Now
Rng.Offset(0, xOffsetColumn).NumberFormat = "dd-mm-yyyy, hh:mm:ss"
Else
Rng.Offset(0, xOffsetColumn).ClearContents
End If
Next
Application.EnableEvents = True
End If
End Sub

This comment was minimized by the moderator on the site
1 26-02-2021, 11:32:49
1 26-02-2021, 11:32:49
1 26-02-2021, 11:32:49
1 26-02-2021, 11:32:49
1 26-02-2021, 11:32:50
1 26-02-2021, 11:32:50
1 26-02-2021, 11:32:50
1 26-02-2021, 11:32:50
1 26-02-2021, 11:32:50
0 26-02-2021, 11:33:04
0 26-02-2021, 11:33:04
0 26-02-2021, 11:33:04
0 26-02-2021, 11:33:04
0 26-02-2021, 11:33:04
0 26-02-2021, 11:33:04
0 26-02-2021, 11:33:04
0 26-02-2021, 11:33:04
0 26-02-2021, 11:33:04
0 26-02-2021, 11:33:04

Why on 0 value its getting date.. when cell have data then only get date otherwise show balnk...how we can do it vba code
This comment was minimized by the moderator on the site
Hi,The code works perfect but if changed the data in A i was wondering if i could have the date in column B and the time in Column D?Thanks for any help.
This comment was minimized by the moderator on the site
Private Sub Worksheet_Change(ByVal Target As Range)
'Update 20140722
Dim WorkRng As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("J:J"), Target)
xOffsetColumn = 1
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each Rng In WorkRng
If Not VBA.IsEmpty(Rng.Value) Then
Rng.Offset(0, xOffsetColumn).Value = Now
Rng.Offset(0, xOffsetColumn).NumberFormat = "dd-mm-yyyy, hh:mm:ss"
Else
Rng.Offset(0, xOffsetColumn).ClearContents
End If
Next
Application.EnableEvents = True
End If
xOffsetColumn = 2
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each Rng In WorkRng
If Not VBA.IsEmpty(Rng.Value) Then
Rng.Offset(0, xOffsetColumn).Value = Now
Rng.Offset(0, xOffsetColumn).NumberFormat = "hh:mm:ss"
Else
Rng.Offset(0, xOffsetColumn).ClearContents
End If
Next
Application.EnableEvents = True
End If
End Sub
This comment was minimized by the moderator on the site
Hi,If i were to have 2 columns duplicating the same macros, how can that be possible.
I want to show the date and time in column B for values changed in A; while also wanting to show the time and date in column D for values changed in C.
This comment was minimized by the moderator on the site
Hi Extend Office, thank you for guiding me.I am a beginner , i had this problem which i saw some of your faced. This was the situationFor e.g
You want your date and time shown in Column A when any other column of the same row B:AZ for example got their values changed.So heres my solution. Please correct me if the code has any issues. TIA.
Private Sub Worksheet_Change(ByVal Target As Range)

Dim WorkRng As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("B:AZ"), Target)'B:AZ put your own columns'rownumber=Activecell.Row will not work because it it will locate the adjacent row after you press enter or when ur mouse click on other cells
rownumber = Target.Row
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each Rng In WorkRng
If Not VBA.IsEmpty(Rng.Value) Then
Range("A" & rownumber).Value = Now 'gives A and the adjacent row number
Range("A" & rownumber).NumberFormat = "dd-mm-yyyy, hh:mm:ss"
End If
Next
Application.EnableEvents = True
End If
End Sub



Thank you.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations