Connecting Tech Pros Worldwide Forums | Help | Site Map

Excel: Variabel for Sheets?

Familiar Sight
 
Join Date: Jun 2008
Location: Sweden
Posts: 130
#1: Sep 3 '08
I'm making a macro in Excel.

In my macro i have something like this in many places:

Expand|Select|Wrap|Line Numbers
  1. Sheet1.Range(Col & Row).Value
  2. Sheet2.Range(Col & Row).Value
  3.  
I whant variables for the Sheets so I can change them in one place and don't need to run a replace on them in the hole module.

Something like
Expand|Select|Wrap|Line Numbers
  1. Dim mySheet1 ' as ? _ mySheet1 = "Sheet1"
  2. Dim mySheet2 ' as ? _ mySheet2 = "Sheet2"
  3. mySheet1.Range(Col & Row).Value
  4. mySheet2.Range(Col & Row).Value
  5.  
Tryied some different things but can't get it working, can you do it by using right thig at "as ?" or can you do it any other way?

FishVal's Avatar
Expert
 
Join Date: Jun 2007
Location: Israel
Posts: 2,584
#2: Sep 3 '08

re: Excel: Variabel for Sheets?


Hello.

You may easily get an answer yourself.
Toggle breakpoint anywhere within the scope of the variable.
In VBE immediate window type:

? TypeName(<...variable name...>)

You will get the variable type.

Also you may add variable to Watch window and get plenty of info about it.

Also I strongly recommend you to use Object Browser (F2 in VBE) to view classes available in referenced libraries.

Kind regards,
Valentine
Familiar Sight
 
Join Date: Jun 2008
Location: Sweden
Posts: 130
#3: Sep 4 '08

re: Excel: Variabel for Sheets?


Quote:

Originally Posted by FishVal

In VBE immediate window type:

? TypeName(<...variable name...>)

You will get the variable type.

Hi

Thanks dident know about that "Interpreter window" before.

I did as you said and got "Worksheet" as answer.

So the declaration is:

Expand|Select|Wrap|Line Numbers
  1. Dim mySheet As Worksheet
  2.  
But then the assignment I don't know how to do, tryied all of this but don't get it working.

Expand|Select|Wrap|Line Numbers
  1. mySheet = ActiveWorkbook.Worksheets(1)
  2. mySheet = ActiveWorkbook.Sheets(1)
  3. mySheet = ActiveWorkbook.Worksheets.Item(1)
  4. mySheet = ActiveWorkbook.Sheets.Item(1)
  5.  
Tryed with <"Sheet1">, <"1"> insted of <1> at index, nothing working. Tryed to find answers in the Object browser and Whatch window but culdn't find anything about assignment of Worksheets.
FishVal's Avatar
Expert
 
Join Date: Jun 2007
Location: Israel
Posts: 2,584
#4: Sep 4 '08

re: Excel: Variabel for Sheets?


Use "Set" operator to assign a value to object variable.

Set objVar1 = objVar2
Familiar Sight
 
Join Date: Jun 2008
Location: Sweden
Posts: 130
#5: Sep 4 '08

re: Excel: Variabel for Sheets?


Quote:

Originally Posted by FishVal

Use "Set" operator to assign a value to object variable.

Set objVar1 = objVar2

Now it works, thanks :)

Hmm newer used set when assigning Integers or Strings so i didn't now about that you needed to use it for "object variables".
FishVal's Avatar
Expert
 
Join Date: Jun 2007
Location: Israel
Posts: 2,584
#6: Sep 4 '08

re: Excel: Variabel for Sheets?


You are welcome.

Regards,
Fish

P.S. BTW there are two operators in Basic to assign value to variable. "Let" is being used for non-object variables and "Set" is used for object ones. "Let" is assumed by default, so usually it is omitted.
Reply