PC Review


Reply
Thread Tools Rate Thread

How do I reset all variables without doing it one at a time?

 
 
Luke
Guest
Posts: n/a
 
      30th Oct 2009
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 With Quote
 
 
 
 
Bernie Deitrick
Guest
Posts: n/a
 
      30th Oct 2009
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.

HTH,
Bernie
MS Excel MVP


"Luke" <(E-Mail Removed)> wrote in message
news:882776D1-8068-4FD8-823A-(E-Mail Removed)...
> 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 With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      30th Oct 2009
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" <(E-Mail Removed)> wrote in message
news:882776D1-8068-4FD8-823A-(E-Mail Removed)...
> 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 With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      30th Oct 2009
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" <(E-Mail Removed)> wrote in message
news:882776D1-8068-4FD8-823A-(E-Mail Removed)...
> 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 With Quote
 
Luke
Guest
Posts: n/a
 
      30th Oct 2009
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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reset variables DG Microsoft Excel Programming 2 8th Nov 2006 02:43 AM
How to reset global variables to nothing Paul Microsoft Access Forms 3 9th Nov 2004 07:51 AM
My variables keep getting reset... ~Jenny Microsoft Excel Misc 0 23rd Aug 2004 02:45 PM
Excel bug, variables reset? RB Smissaert Microsoft Excel Programming 3 31st Jul 2004 03:19 PM
Reset Variables Otto Moehrbach Microsoft Excel Programming 0 8th Jul 2004 08:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:32 PM.