-2

As in title I would like to pass an array to the sub which is executed by a button (That's why I don't know ho to pass it to the sub). Array is calculated in the Sheet before you can exectue sub with the button. I've tried using:

Option Explicit On
Public My_array(1 to 10)

But I have received an error that I can not do that command with array, const etc.

What is the correct way to do it?

1
  • What do you mean "Array is calculated in the Sheet before you can execute sub with the button"? Commented Aug 4, 2014 at 15:14

1 Answer 1

3

I think there is some terminology we can try to clear up: you're not actually passing the variable to the button's procedure. A publicly scoped variable is going to be available within that procedure and does not need to be "passed".

Note: I'm not sure what you mean by "Array is calculated in the Sheet". Unless you are using some event procedure to calculate an array in memory (probably you are not, because you don't have the array scoped or declared properly to do this...), this statement can't possibly be true.

Your declaration should be like:

Public myArray(1 To 10) As Variant 'or As String, As Double, etc.

You can omit the As ... part and it will be type Variant. if you need more strongly-typed array, then you will need to specify.

Also, On is not a legal keyword after the Option Explict statement. So, remove that. But keep Option Explicit in all of your modules.

The error message you receive hints at the solution: You can't put public declarations in object modules. An object module is a Worksheet, Workbook, UserForm, or Class module in the VBE.

You can only put these declarations in a normal code module.

Solution: Move the declaration of myArray to a normal code module.

1
  • 2
    The As Variant part isn't necessary. VB6/VBA will make any variable without a type into a variant.
    – Chel
    Commented Aug 4, 2014 at 15:27

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.