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
 

Some videos you may like

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

joeu2004

Well-known Member
Joined
Mar 2, 2014
Messages
2,974
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,820
Messages
5,598,301
Members
414,224
Latest member
Crazy_FC

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