0

I have a function readLines that reads in 10 lines in an Excel sheet and returns a Dictionary. I want to store the results from this function in a global Dictionary that gets populated at start and then is global available to all other parts of my script.

What I would do in the case of an Integer is to declare the following in an empty module:

Public Cons GLOBAL As Integer = 1

however

Public Cons GLOBAL as Dictionary = readLines

Does not work for this.

Any suggestions?

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
user3748315
  • 149
  • 3
  • 11
  • I think your problem lies with `Cons`. Constant is constant :) So when you declare `Cons = 1 ` it is ok. When you are trying to declare `Cons = readLines` it does not work because you are setting it as a variable. Constant needs to be fixed values like `500`, you cannot change them. Instead of constant try declare your dictionary as variable (so just delete `Cons`) ;) – lowak Aug 26 '14 at 06:29
  • possible duplicate of [Visual Basic global variables in Excel Macro](http://stackoverflow.com/questions/25141505/visual-basic-global-variables-in-excel-macro) –  Aug 26 '14 at 07:13
  • 1
    Plus, it's `Const`, not `Cons`. Plus, global variables/objects are a bad idea 99% of the time. – Jean-François Corbett Aug 26 '14 at 08:35

1 Answers1

2

Actually you can declare your global variable like that:

Public var As Dictionary

(no need for Global, check out about Public,Private,Dim,Global)

Then inside a Sub or something you can initialize it:

Sub test()
    var = readLines
End Sub

You can't call a function to initialize a (global) variable outside a procedure

Community
  • 1
  • 1
smagnan
  • 1,197
  • 15
  • 29