COUNT= number of times opened?

npacker

Board Regular
Joined
Oct 14, 2004
Messages
132
Hi all,
Bear with me here. I have a template that when a user opens it, I have code that automatically does a save as to a different file name in a different folder. But the problem is, I don't want that code to run if the user then goes and opens that file later on. I only want it to run the first time they open the template. I thought maybe I could have it count the number of times it has been opened, and if it is equal to or greater than 1, then not run the code, but then I realized it might restart the count every time they opened that newly saved file every time.

Could I have it save the count to a cell in an invisible sheet, and the pull it from there every time?

I think that would work, but I don't know how to implement the count code on open. Please help!
Nate
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

yee388

Well-known Member
Joined
Mar 7, 2004
Messages
1,374
what about a line like:

If ActiveWorkbook.Name <> "Template.xls" Then Exit Sub

So it'll skip the Open macro if it's not the template...
 

Zack Barresse

MrExcel MVP
Joined
Dec 9, 2003
Messages
10,881
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
  3. Web
Could you use the user name? Write it to a value (cell, registry, etc) then check it upon open? If the current name is not equal to the saved name, run code, else don't run. ??

Here is a good example of getting different user name variables ...

http://www.vbaexpress.com/kb/getarticle.php?kb_id=234

Edit: The only problem with a name check with the original name is dependent on how susceptible the workbook is to name changes. If it will be Saved As at any point, or is an actual template file, I'd suggest another route. Possibly the user name route.
 

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
How about test for the workbook name instead:

<font face=Tahoma><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_Open()
    <SPAN style="color:#00007F">If</SPAN> ActiveWorkbook.Name = "Template.xls" <SPAN style="color:#00007F">Then</SPAN>
        <SPAN style="color:#007F00">'   Run SaveAs code</SPAN>
    Else: <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

HTH,

Smitty
 

npacker

Board Regular
Joined
Oct 14, 2004
Messages
132
That is a great idea, I didn't even think of that, that's perfect. I'll do that.
Thanks,
Nate
 

Forum statistics

Threads
1,148,224
Messages
5,745,472
Members
423,953
Latest member
MrC54

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