Solved

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

Posted on 2015-02-20
12
99 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
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 46

Expert Comment

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

Accepted Solution

by:
Nick67 earned 500 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
 
LVL 46

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 46

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 46

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 46

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

861 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now