2

According to Microsoft: https://msdn.microsoft.com/en-us/library/z2cty7t8(v=vs.100).aspx The following static variable declaration is correct.

Public Sub MyProc()
     static count as integer = 0
     count = count + 1 
End Sub

According to Word 2010 VBA, this is a compiler error. It wants:

Public Sub MyProc2()
     static count as integer
     count = count + 1 
End Sub

You have to assume that the static count is initialized to zero.

Hope this helps someone else.

0

2 Answers 2

4

The link in your question refers to VB.NET, not VBA. VBA requires the syntax as in your second example.

The correct link is Visual Basic for Applications Reference – Static Statement. There the syntax is described as follows:

Static varname[([subscripts])] [As [New] type] [, varname[([subscripts])] [As [New] type]] . . .

As you can see there, VBA doesn't allow assigning a value in the same statement the static variable is declared.

Sign up to request clarification or add additional context in comments.

2 Comments

FWIW, this isn't specific to Static - the only single line declaration\assignment VBA allows is Dim foo As New SomeClass.
Static count As Integer: count = 1 ... declaration and assignment on single line :)
0

Thank you dee, Word VBA 2010 compiler likes this syntax. I prefer my static variables be defined instead of left to the whim of the compiler.

Static count As Integer: count = 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.