Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
How do I reset all variables without doing it one at a time?
The subject speaks for itself. If I have, say, 10 variables and I want to
set them all equal to 0, is there a quick and simple way to do it in one, fell swoop or do I have to do each one individually? I'm pretty sure I'm overlooking something extremely simple here. |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
How do I reset all variables without doing it one at a time?
Luke,
A lot depends on the scope of the variables and what you are doing. If your code is running, and the variables have values that you want to re-set to 0, then you need to do that individually. If you have finished running your code and the variables are declared within the procedure, they will all be re-initialized when you re-run the code, so you don't need to explicitly re-set the values to 0. |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
How do I reset all variables without doing it one at a time?
Not really recommended, but:
End Terminates execution immediately. Never required by itself but may be placed anywhere in a procedure to end code execution, close files opened with the Open statement and to clear variables. Look it up in the VBA help file before using. "Luke" wrote in message ... The subject speaks for itself. If I have, say, 10 variables and I want to set them all equal to 0, is there a quick and simple way to do it in one, fell swoop or do I have to do each one individually? I'm pretty sure I'm overlooking something extremely simple here. |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
How do I reset all variables without doing it one at a time?
You can't reset a group of individually named variables all at once... they
would have to be done one at a time. However, you could make use of an Enum and an array to do what you want... you can "reset" an array using the Erase commands. As a totally made up example, lets say your original variable were named utensils and you assigned two numerical counts and one text count to them, print them out and then clear the variables as follows... Dim Spoons As Long, Knives As String, Forks As Long Spoons = 1 Knives = "Two" Forks = 3 Debug.Print Spoons, Knives, Forks << Clear the variable Debug.Print Spoons, Knives, Forks Of course, the "<< Clear the variable " part is what cannot be done with a single command. However, you could do this code using the concept I outlined above as follows... Enum Utensils Spoons = 0 Knives Forks End Enum Sub Test() Dim U(0 To 2) As Variant U(Spoons) = 1 U(Knives) = "Two" U(Forks) = 3 Debug.Print U(Spoons), U(Knives), U(Forks) Erase C Debug.Print U(Spoons), U(Knives), U(Forks) End Sub You could name the array anything you want... I simply chose U (for the word Utensils) just to keep the code short. Using the Enum allows you to reference the array element by the familiar name you originally used for the variable name. Now, because U is an array, all its elements can be cleared with the single Erase command. As for what Type to dimension the array as... I used Variant because I wanted to demonstrate that different data types could all be erased at once... if your variable were all of the same data type, you could just Dim the array as that data type to make your code more efficient. -- Rick (MVP - Excel) "Luke" wrote in message ... The subject speaks for itself. If I have, say, 10 variables and I want to set them all equal to 0, is there a quick and simple way to do it in one, fell swoop or do I have to do each one individually? I'm pretty sure I'm overlooking something extremely simple here. |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
How do I reset all variables without doing it one at a time?
Thanks, all. I guess I'll just plow through and do them one at a time.
Fortunately there are not very many of them. I was just hoping that, since you can clear/erase practically anything else with just a word, there would be a single statement that would reset all variables to zero. "Luke" wrote: The subject speaks for itself. If I have, say, 10 variables and I want to set them all equal to 0, is there a quick and simple way to do it in one, fell swoop or do I have to do each one individually? I'm pretty sure I'm overlooking something extremely simple here. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Reset variables | Excel Programming | |||
Excel bug, variables reset? | Excel Programming | |||
Reset System Time | Excel Programming | |||
Reset Variables | Excel Programming | |||
Glolbal Variables Reset when using OleObjects | Excel Programming |