Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 29
Default Initializing static variables in VBA

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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Initializing static variables in VBA

Why not check to see if the variable is empty or a value
that you desire, for example:

Sub Counter()

Static counter as Integer

'Assign a value to counter if empty or less than 11.
If counter = "" or counter < 11 Then
counter = 11
End If

counter = counter + 1

'counter now equals 12

Debug.print x

End Sub


-----Original 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


.

  #3   Report Post  
Posted to microsoft.public.excel.programming
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




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 280
Default Initializing static variables in VBA

You cannot initialize a static variable in VB per se, but ...

Sub Counter()
Static counter as Integer

If counter < 12 Then
counter = 12
Else
counter = counter + 1
End If

Debug.print x
End Sub

BTW, a Long has a larger range than an Integer, if it matters.

"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




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
XL 07 Initializing Folders reno Excel Discussion (Misc queries) 0 April 20th 07 07:32 PM
Static Variables Man from Utopia Excel Programming 3 August 8th 04 11:40 PM
Static variables lynnnow[_3_] Excel Programming 3 August 6th 04 07:31 AM
Static variables lynnnow Excel Programming 0 August 6th 04 01:07 AM
Initializing User Forms Tom Ogilvy Excel Programming 3 June 28th 04 10:07 PM


All times are GMT +1. The time now is 10:35 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"