Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   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.


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 08:24 AM.

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

About Us

"It's about Microsoft Excel"