Run VBA code once

jammerdk

New Member
Joined
Feb 3, 2010
Messages
49
Hi guys

Know you can help me on this. :)

I'm trying to run a VBA code when the worksheet is loaded but only want to run it once no matter what.

I'm having this code working but as the worksheet is locked/protected I want to a variable instaid of the cell value in A1.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim N As Long
If Worksheets("Diagram").Range("A1").Text = "Done" Then Exit Sub
'code runs here
Worksheets("Diagram").Range("A1") = "Done"
End if
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
The problem is that variable values are cleared out once your macro(s) finish running. If you want to restrict a macro to run only a single time, you need to have a cell somewhere in your workbook that it can adjust, like what you have now.

What you could do is set up a worksheet and hide that worksheet, using the macro to refer to a cell in that hidden worksheet so it doesn't clutter your data.
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,843
Members
452,948
Latest member
UsmanAli786

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