PDA

View Full Version : Solved: Run Once



MrRhodes2004
01-30-2006, 02:52 PM
I have some code that displays directions for the user but expect the user to use this code more than once. I don't want the instructions to be displayed via a msgbox every time that the code is run.
Is there a way tto set a variable that is toggled when the code is run so that the instructions are not run again?

Example:
Sub MyCode()
If Toggle is False then
Msgbox "Here are instructions"
End if
'Set toggle to true so that Msgbox is not run again
Toggle = True
---Run additional code---
End Sub

XLGibbs
01-30-2006, 03:24 PM
You would have to have the flag outside the scope of the subroutine
Public myFlag as Boolean

Sub Code()

If myFlag = False then
Msgbox "....."
myFlag = True
End If
End Sub


In the workbook_open event set the myFlag to False

Private Sub Workbook_Open()
myFlag = False
End Sub

Something along those lines should do it...otherwise, if you dont have the flag set to False externally, and only set to true on positive IF condition....it will fire...there are other ways as well.

Hope it helps.

Zack Barresse
01-30-2006, 03:34 PM
info

MrRhodes2004
02-09-2006, 02:38 PM
I've added this code but the counter does not seem to maintain the number. When the sub is run again, the counter is reset to zero. Should the StopClickCount as integer be placed elsewhere?


Public StopClickCount As Integer
Sub StopClicking()
Debug.Print StopClickCount
If StopClickCount = 0 Then
MsgBox "There is nothing here for you. Please stop clicking this button."
Else
MsgBox "I've told you all ready. There is nothing here for you so stop clicking this."
MsgBox "You have now clicked " & StopClickCount & " times"
End If

StopClickCount = StopClickCount + 1
Debug.Print StopClickCount
End Sub

xld
02-09-2006, 03:21 PM
It works for me.

You can also use


Sub StopClicking()
Static StopClickCount As Integer
Debug.Print StopClickCount
If StopClickCount = 0 Then
MsgBox "There is nothing here for you. Please stop clicking this button."
Else
MsgBox "I've told you already. There is nothing here for you so stop clicking this."
MsgBox "You have now clicked " & StopClickCount & " times"
End If

StopClickCount = StopClickCount + 1
Debug.Print StopClickCount
End Sub