Changing multiple cells when target updated

IrishDave2137

New Member
Joined
Jun 24, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi,

Hoping this isn't a stupid question but here goes :)

When I run the following code I want a change in column A (Target range) to trigger changes in columns C & D.
When I add a value to column A, the Date will be inserted into column C and Username to column D.
When I clear the value from column A, C & D should also clear.

However I can only get one column to populate using the code below.
If I run the code as provided only column C will change its value.
If I comment out references to 'Target.Offset(0.2)' then column D will populate with a value.
If I change the order in the code I can get column D to populate but not column C.
So in essence it will only change the first target offset and not subsequent offsets.

Does anyone know why this is happening and how I can get both columns C & D to populate when A is changes? Thanks :)

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim WS As Worksheet
Dim UserNameStr As String

Set WS = Sheets("Scan Sheet")
UserNameStr = Application.UserName

WS.Unprotect Password:="########"

If Not Intersect(Target, Range("A:A")) Is Nothing Then

    On Error Resume Next

    If Target.Value = "" Then
        Target.Offset(0, 2) = ""
        Target.Offset(0, 3) = ""
   Else
        Target.Offset(0, 2).Value = Format(Now, "mm/dd/yyyy HH:mm:ss")
        Target.Offset(0, 3).Value = UserNameStr
    End If

End If

WS.Protect Password:="#######", AllowSorting:=True, AllowFiltering:=True

End Sub
 
Well, if disabling the events resolves the issue, I say go ahead and do that.
It is never a bad idea to do that in "Worksheet_Change" event procedure code anyway, when the code is making changes to the sheet.
Absolutely :). Thanks again for the help Joe (& Fluff). Blown away by how quickly you guys responded and helped me out.
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
You are welcome.
Glad we were able to help!
:)
 
Upvote 0
Another option would be
VBA Code:
    If Target.Value = "" Then
        Target.Offset(0, 2) = ""
        Target.Offset(0, 3) = ""
   Else
        Target.Offset(0, 2).Resize(, 2).Value = Array(Format(Now, "mm/dd/yyyy HH:mm:ss"), UserNameStr)
    End If
That way the code is not triggered multiple times, which is what's causing the problem.
 
Upvote 0
Another option would be
VBA Code:
    If Target.Value = "" Then
        Target.Offset(0, 2) = ""
        Target.Offset(0, 3) = ""
   Else
        Target.Offset(0, 2).Resize(, 2).Value = Array(Format(Now, "mm/dd/yyyy HH:mm:ss"), UserNameStr)
    End If
That way the code is not triggered multiple times, which is what's causing the problem.
Thanks Fluff, makes sense. That's a very useful option, thank you.
 
Upvote 0
Hey,

I was using something very similar in order to enter timestamps into a table; the last column of the table works out the duration between each timestamp but, for some reason, this script stops the formula in the last column from copying down as new rows are added to the table. It's the most bizare thing. That column isn't even unlocked for editing and the ranges don't crossover into that column at all. Has anyone experienced this before? Any ideas how to fix?

The taret range is the "NAME" column and my script posts the timestamp into the "TIME ON" column next to the name entered and also the "TIME OFF" column in the row above with the "DUR." column being the duration. I've tested this out and the formula remains fine with no VBA on the sheet but stops working I enter this sub:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)

'The ranges that we are checking is where the names are
If Not Intersect(Target, Range("B10:B100")) Is Nothing Then

On Error Resume Next

'If the value is empty
If Target.Value = "" Then

'Make sure that the relevant cells are empty
Target.Offset(0, 3) = ""
Target.Offset(-1, 4) = ""

Else
'Otherwise put the timestamp in the "TIME ON" & "TIME OFF" cols
Target.Offset(0, 3).Value = Format(Now, "mm/dd/yy HH:mm")
Target.Offset(-1, 4).Value = Format(Now, "mm/dd/yy HH:mm")

End If

End If

End Sub

Cell "B10" is where the "NAME" column starts.
 

Attachments

  • Screenshot 2023-04-04 105937.png
    Screenshot 2023-04-04 105937.png
    2.2 KB · Views: 3
Upvote 0
Hey,

I was using something very similar in order to enter timestamps into a table; the last column of the table works out the duration between each timestamp but, for some reason, this script stops the formula in the last column from copying down as new rows are added to the table. It's the most bizare thing. That column isn't even unlocked for editing and the ranges don't crossover into that column at all. Has anyone experienced this before? Any ideas how to fix?

The taret range is the "NAME" column and my script posts the timestamp into the "TIME ON" column next to the name entered and also the "TIME OFF" column in the row above with the "DUR." column being the duration. I've tested this out and the formula remains fine with no VBA on the sheet but stops working I enter this sub:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)

'The ranges that we are checking is where the names are
If Not Intersect(Target, Range("B10:B100")) Is Nothing Then

On Error Resume Next

'If the value is empty
If Target.Value = "" Then

'Make sure that the relevant cells are empty
Target.Offset(0, 3) = ""
Target.Offset(-1, 4) = ""

Else
'Otherwise put the timestamp in the "TIME ON" & "TIME OFF" cols
Target.Offset(0, 3).Value = Format(Now, "mm/dd/yy HH:mm")
Target.Offset(-1, 4).Value = Format(Now, "mm/dd/yy HH:mm")

End If

End If

End Sub

Cell "B10" is where the "NAME" column starts.
Welcome to the Board!

Since your question is not about the original question, but your own new question, it would be much better to post it to a new thread instead of posting it at the end of an old, existing thread. That way, it will also appear as a new, unanswered question on the "Unanswered threads" list that many people use to look for new, unanswered questions.
 
Upvote 0
Welcome to the Board!

Since your question is not about the original question, but your own new question, it would be much better to post it to a new thread instead of posting it at the end of an old, existing thread. That way, it will also appear as a new, unanswered question on the "Unanswered threads" list that many people use to look for new, unanswered questions.
Yeah, good shout... I'll do that now. Just thought since it's almost he exact same sub that people in this thread might have some idea.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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