Creating timestamp if cell range changes

0rtli

New Member
Joined
Apr 17, 2016
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi guys,
So I need some help solving the issues.
I have data range from A2:Q and if in this range cell is empty R also should be empty, if A2:Q were filled with something, I would like to have a timestamp in column R.
Besides this, data in A2:Q can be copy&pasted so I found an old thread where VBA code works perfectly (without cleaning the timestamp when the cell is empty) but it can't handle when I'm bulk copy&pasting more than 1 row at the same time.

Old thread:
 
Your current code is only set-up to check one cell in one column. That is easy.
Trying to checking multiple columns in multiple rows is a bit trickier.

Try something like this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
    Dim r As Long
    Dim rng2 As Range
 
'   See if any cells in watched range updated
    Set rng = Intersect(Columns("A:Q"), Target)
    If rng Is Nothing Then Exit Sub
 
    Application.EnableEvents = False
 
'   Loop through all cells in range
    For Each cell In rng
'       Get row of update
        r = cell.Row
'       Check to make show update is not in row 1
        If r > 1 Then
'           Build range to check
            Set rng2 = Range(Cells(r, "A"), Cells(r, "Q"))
'           Check to see if any cells in row have a value
            If Application.WorksheetFunction.Count(rng2) > 0 Then
                Cells(cell.Row, "R").Value = Now()
            Else
'               Clear timestamp if all blank
                Cells(cell.Row, "R").ClearContents
            End If
        End If
    Next cell

    Application.EnableEvents = True
 
End Sub
Note that if you are updating multiple columns and rows at once, the code will loop more than it needs to (as it loops through each cell), though it will still work.
Any idea why the timestamp appears only when I enter numbers in range A:Q while using letters the timestamp cell is empty?
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Whoops, I meant to use COUNTA, not COUNT.
COUNT only counts numbers. COUNTA will count anything.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
    Dim r As Long
    Dim rng2 As Range
  
'   See if any cells in watched range updated
    Set rng = Intersect(Columns("A:Q"), Target)
    If rng Is Nothing Then Exit Sub
  
    Application.EnableEvents = False
  
'   Loop through all cells in range
    For Each cell In rng
'       Get row of update
        r = cell.Row
'       Check to make show update is not in row 1
        If r > 1 Then
'           Build range to check
            Set rng2 = Range(Cells(r, "A"), Cells(r, "Q"))
'           Check to see if any cells in row have a value
            If Application.WorksheetFunction.CountA(rng2) > 0 Then
                Cells(cell.Row, "R").Value = Now()
            Else
'               Clear timestamp if all blank
                Cells(cell.Row, "R").ClearContents
            End If
        End If
    Next cell

    Application.EnableEvents = True
  
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,837
Messages
6,181,255
Members
453,028
Latest member
letswriteafairytale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top