PDA

View Full Version : Solved: Counting number of times a code has been executed



jproffer
08-14-2008, 06:27 AM
Well, as you might have guessed, I would like to add a counter to count the number of times a code has been ran. I (think I) understand how to use a counter in a loop:

counter = counter +1

But that doesn't save the last count. At runtime, it's zero until that line is passed, at which time it becomes 1, but then at next runtime, it starts at zero again.

This is not a loop code, so that won't work.

To give a little background too, here's what I'm ultimately trying to accomplish:

I have a button (forms menu button) on a sheet. That button is labeled "Normal". It locks and hides all sheets, makes several toolbars invisible, among other things that don't matter...I can code all that. But I would like to use it as sort of a toggle button. Click once (or on an odd runtime count) and the button caption changes to "Developer" (hides toolbars, scroll bars, sheet tabs, locks sheets, etc.), click again (every even runtime count) and the button caption changes back to "Normal" and goes into "developer mode", basically showing "what will this button do NEXT time I click it"

Also, if there is such a thing as an execution-counter...then how would I determine even or odd...I tried If IsOdd counter....If counter = Odd....it errors out at that line each time.

At any rate, thank you all in advance. Sorry for the long post.

Bob Phillips
08-14-2008, 06:31 AM
Static counter as long

counter = counter + 1

RonMcK
08-14-2008, 09:31 AM
Use the following to sort out your Odd and Even instances:
If Counter Mod 2 = True Then
' do whatever you want to do when Counter is Odd
Else
' do whatever you want to do when Counter is Even
End If Cheers,

jproffer
08-14-2008, 10:57 AM
Those are definately two huge steps in the right direction. The counter works as expected now. But the odd even instances always goes to the Else result of the If statement. Here's what I have and what it's doing.

Sub Test()
Static counter As Long
counter = counter + 1
If counter Mod 2 = True Then
ActiveSheet.Shapes("Button 3").Select
Selection.Characters.Text = "Normal"
Range("A1").Select
Else
ActiveSheet.Shapes("Button 3").Select
Selection.Characters.Text = "Developer"
Range("A1").Select
End If
End Sub

On a manual walk-thru (debug):

After line 3, the counter has been stepped, and holds it's value thru several runtimes (I think about 100 or so), so I'm sure that issue is resolved.
On line 4, when it evaluates the counter value, it steps down to the Else result on line 8 every time, regardless of the counter value. I check the counter value (hover over it) after every complete step-thru and it continues to go to the Else result, to execute the Even Command.

This obviously isn't the entire code or intent of this, but just seeing the button caption toggle would be nice, lol...hence this "test" sub.

Again, thank you both for your help. I'm going to keep looking, but if you have any ideas, I'm all ears...er uhh...eyes. :-)

Bob Phillips
08-14-2008, 11:35 AM
counter Mod 2 will never be true as it will always return 0 or 1 and True is -1 in VBA.

Use



If counter Mod 2 = 1 Then

jproffer
08-14-2008, 12:05 PM
That got it...thank you all for your help. I (again...THINK I) can take it from here. If I'm wrong about myself, you all will be the first to know, lol.

RonMcK
08-14-2008, 12:10 PM
Thanks, Bob. Yet one more difference between VBA and other Basics for me to remember. Arrggghhhh! (Quoting Wiley of B.C. comic strip)

Cheers!