Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

How to determine changing a column in a row

0

I use the code below to dermine if a cell has changed.

If so put date+time in column "A"

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("B:BB")) Is Nothing Then Range("A" & Target.Row) = Date + Time

End Sub

When I double click a cell, it will be determined as a change, but that's not what I want.

Wich code determines a real change of value?

Answer
Discuss

Answers

0

Hi and welcome to the Forum

The commented code below will work if a single cell is changed (which it checks). Note that I've used Now rather than Date + Time

Private Sub Worksheet_Change(ByVal Target As Range)

' do nothing if not a single cell in range
If Target.CountLarge <> 1 Or Intersect(Target, Range("B:BB")) Is Nothing Then Exit Sub

NewVal = Target.Value

Application.EnableEvents = False 'stop undo triggering this again

Application.Undo 'get old value
If Target.Value <> NewVal Then
    Application.Undo 'restore new value
    Range("A" & Target.Row) = Now
End If

Application.EnableEvents = True
End Sub
To cover the case when Enter is not used to change the value, e.g. you click in another cell, also add this event macro:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Target.Select

End Sub
It just puts the cursor in the new cell (and lets the Change event macro add the data and time).
Discuss

Discussion

One additional question.
The column value is 1
When I change the column value in 2, column "A" filled with Now. Top.
Only I have to click/enter a second time to leave the cell.
In detail:
- A cell (column K, row 5) contains the value 1
- I move to that cell using the mouse
- I change the value into 2
- I click column P, row 6
- Column "A" (row 5) filled with Now. Correct
- but the mouse dint't change to P6
- I have to click once more, to move to P6

I can't leave the cell after change of value.

A tip?
wvduuren Dec 8, '21 at 9:32 am
See revised Answer, sorry I forgot to add the extra bit.
John_Ru (rep: 6142) Dec 8, '21 at 9:42 am
Did you try that?
John_Ru (rep: 6142) Dec 9, '21 at 8:43 am
Is it working correctly now? 
John_Ru (rep: 6142) Dec 13, '21 at 4:48 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login