Run a macro only if its the first time opening a model?

squnibi

Board Regular
Joined
May 26, 2010
Messages
74
Hi

How would I be able to set up a code that will run a macro only if it is the first time you open the model that day?
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Set a trap in a cell, ie when you run the macro it adds a date or something in a cell, then the next time it runs check the cell to see if there is a content or latest date and if then exist the sub, else run it.
 
Upvote 0
Thanks for the reply. Can you be a little more specific though? I already have created a named range in excel that calculates the current time. Now I want a code that will look at this date and if it is the first time the model is open, then I would like it to run another macro.
 
Upvote 0
What he means is you would choose a cell somewhere in the workbook, and when the sheet opens it would check the cell and if today's date were more recent than it, insert a new date and run the macro. If it were older or the same it would exit the sub.
 
Upvote 0
Yes I understand that part. I already have a cell in place that does this but need help with the actual code. I'm pretty new to VBA
 
Upvote 0
You might need to show your code to help complete your task but something like this might work for you

Sub CheckCell()
IF range("CellID").value=Date Then
Exit Sub
Else
'Run your code
End Sub
 
Upvote 0
Yeah, something to that effect. Remember you can call your macro by name if you like, so you can have your macro be separate from this one.

I'd probably go with something like:
Code:
Sub CheckCell()

With Range("CellID")
If .value < Date Then
     'Name of macro you want to run
     .value = Date
End If
End Sub
 
Upvote 0
Won't work if the user closes the model without saving changes....
 
Upvote 0
Is that a bad thing though? Maybe he doesn't want the effects of the macro set in stone, or it may be a good thing that it runs again when the workbook is reopened. Depends on what he's trying to do.
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,337
Members
452,907
Latest member
Roland Deschain

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