Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 142
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default 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   Report Post  
Posted to microsoft.public.excel.programming
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.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 142
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reset variables DG Excel Programming 2 November 8th 06 02:43 AM
Excel bug, variables reset? RB Smissaert Excel Programming 3 July 31st 04 03:19 PM
Reset System Time Martin[_13_] Excel Programming 2 July 19th 04 11:36 PM
Reset Variables Otto Moehrbach[_6_] Excel Programming 0 July 8th 04 08:50 PM
Glolbal Variables Reset when using OleObjects Rock Excel Programming 11 May 31st 04 03:00 AM


All times are GMT +1. The time now is 10:53 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"