Write offset formula reference in VBA code

Jyggalag

Active Member
Joined
Mar 8, 2021
Messages
445
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi all,

I have this formula:

1652449248970.png


Essentially, when I fill in ANY data or make ANY changes to cell D2, i want the VBA to automatically update the current date/time in cell B1, so I can see the last time I made any update to cell D2.

I can do xOffsetColumn = -2

But this will show the date in cell B2, not B1.

Also, referencing the code to "B1" does not work, as you can see above.

Does anybody know how to fix my code so I can refer to cell B1?

Thank you! :)
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
xOffsetColumn=-2 ---you don't need to change this line.
Rng.offset(-1,xOffsetColumn) ---Three lines should be like this.
 
Upvote 0
Throw away that code and use this.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

   If Not Intersect([D2], Target) Is Nothing Then
      Application.EnableEvents = False
      [B1] = Date
      Application.EnableEvents = True
   End If

End Sub
 
Upvote 0
Solution
Also note that is you declare xOffsetColumn as an Integer (like you did in your code), you cannot set it equal to "B1", as "B1" is text, NOT an integer!
 
Upvote 0
tri
xOffsetColumn=-2 ---you don't need to change this line.
Rng.offset(-1,xOffsetColumn) ---Three lines should be like this.
tried this :( didnt work, it gives an error for "Rng.offset(-1, xOffsetColumn)
 
Upvote 0
Throw away that code and use this.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

   If Not Intersect([D2], Target) Is Nothing Then
      Application.EnableEvents = False
      [B1] = Date
      Application.EnableEvents = True
   End If

End Sub
This is great and it works!

But it only shows the date, not the time, any way to fix this?
 
Upvote 0

Forum statistics

Threads
1,223,785
Messages
6,174,539
Members
452,571
Latest member
MarExcelTips

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