In a perfect world, I wouldn't have to do this, but I'm at the mercy of event scripting for a program.
there's an event that runs every so often that triggers an subroutine. I'd like to declare a variable in that subroutine, but not have to worry about it when an event triggers it again. If I just go:
Public variable As String
.. it will give an error. I need to check if the variable exists... can that actually be done? I've yet to see a method to check if a variable exists.
plz help. Thanks.
Dim variable as string
is not a executable line. it doesnt matter if you declare it one million times, it wont have any effect (only that each time the variable will be reset) if you want it not to reset, define it outside the Sub.
Well, actually it's going to make your code a little slower, It's going to take 1 more second to run for each 10 million times (aprox) you declare a variable. Here, check this out:
-
Option Explicit
-
Dim n As Integer
-
Sub One()
-
Dim i As Integer
-
For i = 1 To 10: i = i: Next
-
End Sub
-
Sub Two()
-
For n = 1 To 10: n = n: Next
-
End Sub
-
Sub Test()
-
Dim t1 As Single: Dim t2 As Single
-
Dim j As Long
-
t1 = Timer
-
For j = 1 To 10000000: Call One: Next
-
t1 = Timer - t1
-
t2 = Timer
-
For j = 1 To 10000000: Call Two: Next
-
t2 = Timer - t2
-
MsgBox t1 & Chr(13) & t2
-
End Sub
^.^ It's somekind of fun experiment.
anyway, im not even sure that you can check if a variable exists if it doesnt exist yet. i.e. you cannot say IF variable something THEN declare variable. Since it didnt exist at the begining of the IF (or if you're not using option explicit, in that very moment it'll become a variant (object in vb2005))
HTH