View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default 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.