Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2015-02-20
12
Medium Priority
?
280 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

610 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