Keeping a Public variable always available

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
918
Office Version
  1. 365
Platform
  1. Windows
I have this
Code:
Public colnum As Long
Sub Callcolnum()
colnum = Round((Date - DateValue("12/20/2017")) / 7, 0)
MsgBox colnum
End Sub
in a modole renamed to COLNUMDECLARATIONCODE
and this
Code:
Private Sub Workbook_Open()
Callcolnum
at the beginning of ThisWorkbook code. Everything works fine - until this issue...

Issue: as long as the application is live and running colnum is correct and available to all code modules.

But if I have to make an editing change or something else, which causes the application to be put in the DESIGN mode, and then be put back to the live app from the Developer tab, the variable's value no longer exists and defaults to 0.

Is there a way to always keep the correct value of colnum available when popping in and out of Design without having to initiate a Save -> Close ->Reopen the app to regenerate the correct value of colnum?


Thanks for anyone's help

cr
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
That is one of the problems with Public/Global variables. In your case I would get rid of the global variable colnum and the Callcolnum line in Workbook_Open and change Sub Callcolnum to Function colnum, like this:

Code:
Function colnum()
colnum = Round((Date - DateValue("12/20/2017")) / 7, 0)
MsgBox colnum
End Function
Then any other instances of colnum in your code will call the above function, instead of referring to the global variable.
 
Upvote 0
Which is exactly what I suggested in his original thread... ;)
 
Upvote 0
I did exactly what you said - and it works! I have you and RoryA, the Moderator to thank. As per his comment below, he suggested I do this yesterday.
One last comment:
It seems to me that, when you have a complete understanding of everything VBA is capable of doing, like you and RoryA do, it can greatly reduce the amount of coding and eliminate redundancy into fewer lines in a compact, neat and simple package when building an application.

Thanks again to you both.
Merry Christmas and Happy New Year
cr
Kingwood, Texas
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,118
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top