How to refer to multiple variables

amauer

New Member
Joined
May 11, 2012
Messages
33
I am trying to select a range of rows, then hide or unhide them. here is some sample code:

Dim Var1 as range
Dim Var2 as range

Set Var1 = Sheets("Sample").Range("22:25")
Set Var2 = Sheets("Sample").Range("28:33")

Range("Var1,Var2").EntireRow.Hidden = True (this is the error line)

I get an error, but it will work if I do this:

Var1.EntireRow.Hidden = True

The issue is that my list of varibles is 28 and must be interchangeable.

Thanks for any suggestions!
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try it without the quotes

That will hide the rows in between also though. Otherwise you can have one array variable and save the ranges there, then loop through.
 
Last edited:
Upvote 0
I removed the quotes, Range(Var1,Var2).En...., but I still receive the same error. I also get the error when I only have one variable.

Looping is an issue, my variables have descriptive names.
 
Upvote 0
This worked just fine for me:
Code:
Dim Var1 As Range
Dim Var2 As Range


Set Var1 = Sheets("Sample").Range("22:25")
Set Var2 = Sheets("Sample").Range("28:33")


Range(Var1, Var2).EntireRow.Hidden = True
What is the exact error message you get when trying this?
 
Upvote 0
Do you have a sheet named "Sample"?
Do you have any merged cells on the "Sample" sheet?
Are there any protected ranges on the sheet?
 
Upvote 0
Yes, sheet name is correct.
I have merged cells, but the merges do not overlap the ranges.
Nothing is protected.

I am able to use Var1.EntireRow.Hidden = True, but would like to simply by not adding a list of these. I would like to incorporate the line with all variables I need.
 
Upvote 0
Check to see if this works:
Code:
[COLOR=#333333]Var2.EntireRow.Hidden = True[/COLOR]
If that doesn't, then you know where to look for issues.
 
Upvote 0
This worked just fine for me:
Code:
Dim Var1 As Range
Dim Var2 As Range


Set Var1 = Sheets("Sample").Range("22:25")
Set Var2 = Sheets("Sample").Range("28:33")


Range(Var1, Var2).EntireRow.Hidden = True
It worked fine for me as well. Not sure where the error will turn out to be, but I wanted to point out to the OP that he did not need to use two separate variables to do his hiding... this also works.
Code:
Dim Var As Range

Set Var = Sheets("Sample").Range("22:25,28:33")

Var.EntireRow.Hidden = True
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top