Search code examples
excelvbascopeglobal-variables

How do I declare a global variable in VBA?


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?


Solution

  • 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