Run-once macro

G

Guest

Hi All...........
Is there a way to have a SelectionChange macro set up so it will run the
first time it's called upon, and as a last item, disable itself to never run
again unless "reset"?

TIA
Vaya con Dios,
Chuck, CABGx3
 
G

Guest

You could have the macro, as the first step, check what ever unique
condition it imposes and if it appears it has run, then exit.

It will still run, but will exit without doing anything.
 
J

Jeff Standen

There are probably a number of ways, but I would get it to create a Name
(hidden) with a particular reference when it runs. Then, at the start of the
SelectionChange event you could look at the name to see the value and run it
or not based on that.

Jeff
 
C

Chip Pearson

Chuck,

Try something like


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Res As Variant
On Error Resume Next
Res = ThisWorkbook.Names("RunOnce").Value
If IsEmpty(Res) Then
MsgBox "Run Once Code"
End If
ThisWorkbook.Names.Add "RunOnce", "TRUE"

End Sub

You can reset it by deleting the name "RunOnce"

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
G

Guest

Cool........that should work for my immediate requirement.........

Thanks, Tom

Vaya con Dios,
Chuck, CABGx3
 
G

Guest

Ah-ha..........increased security......

Thanks, Jeff

Vaya con Dios,
Chuck, CABGx3
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top