How do I declare a global variable in VBA?

Nimrod picture Nimrod · Apr 27, 2010 · Viewed 1M times · Source

I wrote the following code:

Function find_results_idle()

    Public iRaw As Integer
    Public iColumn As Integer
    iRaw = 1
    iColumn = 1

And I get the error message:

"invalid attribute in Sub or Function"

Do you know what I did wrong?

I tried to use Global instead of Public, but got the same problem.

I tried to declare the function itself as `Public, but that also did no good.

What do I need to do to create the global variable?

Answer

SLaks picture SLaks · Apr 27, 2010

You need to declare the variables outside the function:

Public iRaw As Integer
Public iColumn As Integer

Function find_results_idle()
    iRaw = 1
    iColumn = 1