How to make macro run only once

Hiport

Active Member
Joined
May 9, 2008
Messages
455
Hi, is it possible to put a message box in a VBA code so that the macro only runs once, else the worbook needs to reopened again for it to work. Is this the best way from preventing the macro to run again?

If i have multiple users, there is always a chance one might accidently press the command button twice.
 
Did you try as mentioned in post #5?

Private Sub Workbook_Open()
Sheets("YourSheetName").CommandButton1.Enabled = True
End Sub

Private Sub CommandButton1_Click()
Sheets("YourSheetName").CommandButton1.Enabled = False
End Sub

i get the same error message as previous post. Let me get this straight, is the below correct?

Code:
Private Sub Workbook_Open()
Sheets("Sheet1").CommandButton1.Enabled = True
End Sub


Code:
Private Sub CommandButton1_Click()
    Range("A1").Value = "Hello"
   Sheets("Sheet1").CommandButton1.Enabled = False
End Sub
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hiport

Are you certain that your sheet is called 'Sheet1' with no spaces (including before or after the name)?

Can you confirm that the button was added by Developer ribbon|Insert|ActiveX Controls command button?

In design mode, right click on the button and choose 'Properties' can you confirm the name (very top line) is CommandButton1?
 
Upvote 0

Forum statistics

Threads
1,224,540
Messages
6,179,417
Members
452,912
Latest member
alicemil

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