Share via

Declaring Constants in an Array

Anonymous
2013-10-07T12:46:01+00:00

I wish to declare a 'fixed' range of co-ordinates. Something like ...

Public arrCoord1 as Integer

Public arrCoord2 as Integer

arrCoord1 = Array(500, 600, 50, 100)

arrCoord2 = Array(600, 700, 60, 110)

There are multiple sets of co-ordinates that are accessed frequently across several procedures.

They may change, but only if the design were to change. They need to be accessible and documented within a group and not scattered through the project. That's why I want to declare them once at the top of the code.

The above code does not compile.

Should they be Contstants?

Do they need to be Public? 

Whats the best approach and coding solution?

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2013-10-07T13:55:12+00:00

Re:  "I tried the Variant declaration before I changed it to Integer. Neither worked!"

You need to declare values that are fixed as Constants.

The syntax in a class module is:

   Private Const lngVariable As Long = 999

The syntax in a standard module is: 

   Public Const lngVariable As Long = 999

Unfortunately, VBA will not accept an array as a constant.

Also, class modules are private modules, so anything in them is not available to other modules unless the module name is specified and that only works when the variable is declared as public.

Move your declarations to a standard module...

  Public arrCoord1 as Variant

  Public arrCoord2 as Variant

Then in your UserForm Initialize event or in a Sub that is called when the workbook opens, you can initialize the arrays...

Private Sub UserForm_Initialize()

  arrCoord1 = Array(500, 600, 50, 100)

  arrCoord2 = Array(600, 700, 60, 110)

End Sub

'---

Jim Cone

Portland, Oregon USA

https://goo.gl/IUQUN2 (Dropbox)

(free & commercial excel add-ins & workbooks)

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Anonymous
2013-10-07T13:19:18+00:00

Use Variant arrays as follows:

Public arrCoord1 As Variant

Public arrCoord2 As Variant

Sub MAIN()

Call Initializee

Call followup

End Sub

Sub Initializee()

arrCoord1 = Array(500, 600, 50, 100)

arrCoord2 = Array(600, 700, 60, 110)

End Sub

Sub followup()

MsgBox arrCoord1(0)

End Sub

The values will be available to all subs in the module

Was this answer helpful?

0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2013-10-07T13:35:04+00:00

    If I use .....

    Dim arrCoord1 as Variant

    Dim arrCoord2 as Variant

    arrCoord1 = Array(500, 600, 50, 100)

    arrCoord2 = Array(600, 700, 60, 110)

    .... in every procedure; it compliles OK!

    I just want to remove the duplication and place the code in one (public ??) place.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2013-10-07T13:08:54+00:00

    I am running this as part of a UserForm.

    'Compile Error: Invalid outside procedure' 

    I tried the Variant declaration before I changed it to Integer. Neither worked!

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2013-10-07T13:02:56+00:00

    Martyn,

    Re:  "The above code does not compile."

    Because an Integer cannot contain an Array.

    Change the declaration to Variant.

    '---

    Jim Cone

    Portland, Oregon USA

    https://goo.gl/IUQUN2 (Dropbox)

    (free & commercial excel add-ins & workbooks)

    Was this answer helpful?

    0 comments No comments