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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
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,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

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