?
Solved

How to I maintain a variables scope in VBA until the workbook closes?

Posted on 2015-02-20
12
Medium Priority
?
220 Views
Last Modified: 2016-02-10
I want the variable myInteger to have a permanent scope so that I can set it to whatever I want when the workbook opens, and it will be ready for use for whenever I run a separate macro. I noticed that the variable loses its value as soon as the macro below is complete. Is there any way to keep it in Excel's memory as the value I want, until I close the workbook?

Public myInteger As Integer

Public Sub Workbook_Open()

    myInteger = 1

End Sub

Open in new window

0
Comment
Question by:shampouya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 49

Expert Comment

by:Martin Liss
ID: 40622468
Move the Public myInteger definition in a code module.
0
 
LVL 26

Accepted Solution

by:
Nick67 earned 2000 total points
ID: 40622515
Be warned.
Even if you make it Public in scope in a code module, ANY run-time error will set it to null.

You are better off to put it in a cell in a 'very hidden' sheet, and then retrieve it from the cell.
0
 
LVL 2

Expert Comment

by:Rossano Praderi
ID: 40622723
The comment of Nick67 is the best choice, for your convenience I've attached a sample code.

Bregs
Rossano Praderi
Public-Variables.xlsm
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 49

Expert Comment

by:Martin Liss
ID: 40623185
Nick67's remark does not seem to be true. Run Sub1 and then Sub2.

Option Explicit

Public MyVar As Integer

Sub sub1()
MyVar = 99
End Sub
Sub sub2()
Dim i As Integer
On Error GoTo ErrorRoutine
MsgBox "Myvar's value before the error is " & MyVar
' Generate a runtime error (division by zero)
i = 7 / 0
Exit Sub
ErrorRoutine:
MsgBox "Myvar's value after the error is " & MyVar

End Sub

Open in new window

0
 
LVL 9

Expert Comment

by:Ramanhp
ID: 40623516
Try using the concept of global variables.

Steps : Insert New Module from VBA Project UI and declare variables using Global

Declation Example :
Global iRaw As Integer
Global iColumn As Integer
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 40624149
@Ramanhp: While Global still works, it was replaced by Public a long time ago and in any case they both work the same way.
0
 
LVL 9

Expert Comment

by:Ramanhp
ID: 40624341
Thanks Martin.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40626165
@Martin Liss
You successfully handled the error.
On Error Resume Next and unhandled errors, can be persnickety.

Murphy's law will apply.
The odds that a global variable will get dumped are proportional to the annoyance it's unanticipated dumping will cause.
You can build code that will check a global variable for validity before using it...
Or just not use a global variable, and store what is needed in a cell.

It's the kind of thing that, if you've encountered it, you remember and don't get fooled again.
YMMV

Nick67
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 40626206
On Error Resume Next and unhandled errors, can be persnickety.
There's no On error Resume Next in the code I posted.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40626215
Right
On Error GoTo ErrorRoutine
You handled the error.
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 40626253
I see now that you are right about it becoming 0 and it even happens if you use a Private Property for the variable, which is really surprising to me. - thanks.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40626264
You're welcome.
It isn't that Public variables in a global scope don't have their uses.
There are times when you need to preserve and pass a value from a routine that you know is going to end.
You just have  to make damn sure that between the time you set it and the time you consume it, that nothing can go wrong.

From opening until closing is a long time to ensure nothing squirrels up the works :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question