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

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
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.
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,659
Members
449,091
Latest member
peppernaut

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