Consulting

Results 1 to 7 of 7

Thread: Solved: Counting number of times a code has been executed

  1. #1
    VBAX Regular
    Joined
    Dec 2004
    Posts
    92
    Location

    Solved: Counting number of times a code has been executed

    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:

    [vba]counter = counter +1[/vba]

    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.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]
    Static counter as long

    counter = counter + 1
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location
    Use the following to sort out your Odd and Even instances:
    [vba]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[/vba] Cheers,
    Ron
    Windermere, FL

  4. #4
    VBAX Regular
    Joined
    Dec 2004
    Posts
    92
    Location
    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.

    [vba]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[/vba]

    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. :-)

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    counter Mod 2 will never be true as it will always return 0 or 1 and True is -1 in VBA.

    Use

    [vba]

    If counter Mod 2 = 1 Then
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Regular
    Joined
    Dec 2004
    Posts
    92
    Location
    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.

  7. #7
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location
    Thanks, Bob. Yet one more difference between VBA and other Basics for me to remember. Arrggghhhh! (Quoting Wiley of B.C. comic strip)

    Cheers!
    Ron
    Windermere, FL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •