Global variable being reset, Is there a smarter wat to retain a variables value?.

raven_squire

Board Regular
Joined
Jan 13, 2013
Messages
99
Hello,

In my code I have a global variable that gets calculated when the workbook opens. A macro copies data that the user has entered into a table based on the value of the variable. Then it changes the value of the variable each time it runs. The Macro is triggered when the user makes changes to cretin parts of the spreadsheet.

I have noticed that occasionally data is being copped over the column names of the table. I have noticed it happens after different macro error events or the reset button is pressed. This seams to reset the variable to 0 causing the data to be copied over the column names of the table.

Is there a way of making sure that the value of the variable is not ever lost?
I had thought about placing the value in a cell but that seams like a dirty fix. I also don't have a convenient place to put the variable.

Just wondering what the best practice is in this situation??

Thanks
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

joeu2004

Banned user
Joined
Mar 2, 2014
Messages
3,080
Office Version
  1. 2010
Platform
  1. Windows
This seams to reset the variable to 0 causing the data to be copied over the column names of the table. Is there a way of making sure that the value of the variable is not ever lost? I had thought about placing the value in a cell but that seams like a dirty fix. [....] Just wondering what the best practice is in this situation?

That's the cleanest solution that I know of. You can create a worksheet just for this purpose. You can hide and/or protect the worksheet, if you wish.

An alternative is to write the variable to a data file. But that is a dirtier fix, IMHO.

Yes, VBA resets global and static (as well as non-static local) variables to zero sometimes, often beyond our control.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,026
Messages
5,835,024
Members
430,332
Latest member
Charly_Moon

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
Top