VBA Macro

Klturi421

Board Regular
Joined
Aug 31, 2014
Messages
52
I am looking to have cell B1 update with the current date anytime any of the cells are updated on the workbook. Naturally, I don't want to cause an infinite loop with updating B1.

I know I've seen it done before, I just cant figure out where the macro is that I used for it.

Thanks in advance.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

tygrrboi

Well-known Member
Joined
Sep 8, 2015
Messages
1,196
You mean if any sheet is changed, that sheet gets a value in B1? or one specific sheet gets a value in B1?


Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)


Application.EnableEvents = False
    Sh.Range("B1").Value = Format(Now(), "mm/dd/yyyy")
[COLOR=#008000]    'alternatively, use Sheets("sheetname").Range("[/COLOR][COLOR=#008000]B1") = Format(Now(), "mm/dd/yyyy")[/COLOR]
Application.EnableEvents = True


End Sub
 
Last edited:
Upvote 0

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,591
Office Version
  1. 365
  2. 2019
  3. 2013
  4. 2007
Platform
  1. Windows
maybe this in the sheet module


Code:
Sub worksheet_change(ByVal target As Range)
Application.EnableEvents = False
Range("B1").Value = Now()
Application.EnableEvents = True
End Sub
 
Upvote 0

Klturi421

Board Regular
Joined
Aug 31, 2014
Messages
52
My apologies, only the sheet that is being updated at the time.

Also, I tried using the code you provided but it doesn't seem to be working. Am I missing anything? I tried using the alternate one that you provided and changed the "sheetname" to the appropriate sheetname but it still did not work.
 
Upvote 0

tygrrboi

Well-known Member
Joined
Sep 8, 2015
Messages
1,196
My question is... does this apply to all sheets? or just the one sheet?
I know its only one at a time... but do you want for example, an edit in Sheet1 to show in Sheet1!B1 and an edit in Sheet2 to show in Sheet2!B1? Or do you just want edits in sheet1 to show and ignore edits in other sheets?

I have edited my original response because I hadnt tested it and it was resulting in an error... but the above should work if it applies to all sheets. It needs to go into the code for ThisWorkbook.

If it only applies to one sheet... I would go with Michael's code in that sheet's VBA window.
 
Upvote 0

Klturi421

Board Regular
Joined
Aug 31, 2014
Messages
52
My question is... does this apply to all sheets? or just the one sheet?
I know its only one at a time... but do you want for example, an edit in Sheet1 to show in Sheet1!B1 and an edit in Sheet2 to show in Sheet2!B1? Or do you just want edits in sheet1 to show and ignore edits in other sheets?

I have edited my original response because I hadnt tested it and it was resulting in an error... but the above should work if it applies to all sheets. It needs to go into the code for ThisWorkbook.

If it only applies to one sheet... I would go with Michael's code in that sheet's VBA window.
As I stated in my reply initially, it only applies to the sheet being updated. I tried both codes that were provided and it still does not work for me. I'm not sure what I'm doing wrong besides updating the sheetname section.

I will keep trying and report back with my result.
 
Upvote 0

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,591
Office Version
  1. 365
  2. 2019
  3. 2013
  4. 2007
Platform
  1. Windows
where are you pasting the code ??
It needs to be pasted in the relevant Sheet module .......NOT this workbook module !!
My code will not need to be modified
 
Last edited:
Upvote 0

tygrrboi

Well-known Member
Joined
Sep 8, 2015
Messages
1,196
You have two codes now. Between the one I provided and the one Michael provided you have a solution.

You can now either google or youtube how and where to put a worksheet_change event or workbook_sheetchange event so you can be sure its in the right place.

Another option, if you are certain that it is in the correct place and it is not running, is to make sure EnableEvents is on from the start. Otherwise neither of those will run.

In your VBA Immediate Window, type
Application.EnableEvents = True
hit enter then go back to your sheet to test it out.
 
Upvote 0

Forum statistics

Threads
1,191,000
Messages
5,984,082
Members
439,872
Latest member
noaman79

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
Top