View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Initializing static variables in VBA

Sub Test()

Call counter(12)
Call counter
Call counter

End Sub


Sub counter(Optional InitSet As Variant)

Static counter As Integer
If Not IsMissing(InitSet) Then
counter = InitSet - 1
End If
counter = counter + 1

Debug.Print counter

End Sub

--
Regards,
Tom Ogilvy

"Adrian" wrote in message
...
Hi,

I am writing a static procedure as follows:

Sub Test

call Counter
call Counter
call Counter

End


Sub Counter()

Static counter as Integer

counter = counter + 1

Debug.print x

End Sub

I want to print a series of numbers 12,13,14, from the
test procedure instead of 1,2,3 . How do i go about doing it ?
In other words, how do i initialize a static variable in a Sub procedure ?

Thanks.

--
Regards,
Adrian