Disable Macros after one has been run

wyeah

Board Regular
Joined
Jun 23, 2003
Messages
92
I have a spreadsheet that has several macros. Is it possible to disable these macros once one of them has been run???

This is an Excel 2002 file running on Windows XP.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try this:

In the General Declarations section of the module put this:

Public flag As Integer

At the beginning of each of your macros, put this:

If flag = 1 Then Exit Sub

At the end of each of your macros, put this:

flag=1
 
Upvote 0
This works great and answers my first question. Now, when I save this file as a new file name, close this file, and open it again all the macros are enabled again. Is there a way to permenantly disable the macros after 1 use??? I know I could tell people to not enable macros and not run any macros when they open the file again but they know this already and it still happens.

What I am doing is filling in a weekly work schedule with a list of people based on the week of the year. I then enter in vacations and absences and such. But if someone runs one of the macros with a different list of people, or the same macro, then it overwrites all the absences information that I put in. Thanks
 
Upvote 0
Keeping what we did before, try adding these two things:

To the Workbook Before Save Event:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If flag = 1 Then
Range("IV65536").Font.Color = vbWhite
Range("IV65536").Value = 1
End If
End Sub

To the Worbook Open Event:

Private Sub Workbook_Open()
If Range("IV65536").Value = 1 Then flag = 1
End Sub
 
Upvote 0
HOTPEPPER'S solution works great. The only drawback I can see is what you've mentioned.
You could solve this with a very similar method by changing the flag reference to an unused (hidden if you want) cell. Example:

At the beginning of each of your macros, put this:
If [A1] = 1 Then Exit Sub

At the end of each of your macros, put this:
[A1]=1

This help?
Dan

Edit: I see while I went for a cup of coffee, HOTPEPPER got you squared away. 8-)
 
Upvote 0
HOTPEPPER,

I'm trying your code above, but can't get it to work. Can you please help?

I'm using a macro to pull the username of the person logged in. However, when the spreadsheet is opened again at a later time, it updates with that person's username and we need it to keep the original.

Here's my code:

Public flag As Integer
If flag = 1 Then Exit Sub
Sub Auto_Open()
Range("G8").Value = Environ("UserName")
flag = 1
End Sub
 
Upvote 0
I'm probably bringing this post back from the dead - but thank you for this solution! It works perfectly!
 
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,580
Members
452,653
Latest member
craigje92

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