calculate a range based on a variable?

Gingertrees

Well-known Member
Joined
Sep 21, 2009
Messages
697
I have a command button that hides several ranges of rows when clicked, and I want to make this simpler to edit by defining the starts and ends of ranges using variables.

So my code now is:
Code:
 Rows.("59:100").EntireRow.Hide = true 'range 1
Rows.("125:145").EntireRow.Hide = true 'range 2 
'///ranges here are 25 rows apart

WHERE I GET STUCK:
How do I tell the computer that the rows I want to hide are defined by variable? Here's some code-that-doesn't-work to show what I want:
Code:
Private Sub CommandButton1_Click()
Dim Rg1Start As Long '1st row of 1st range
Rg1Start = 59
Dim Rg1end As Long 'last row in range 1
Rg1end = 100
Dim Distance1 As Long 'distance between range 1 and 2
Distance1 = 35 '///////////since I added some rows
Dim Rg2Start As Long
Rg2Start = Rg1end + Distance1 'beginning of range 2
Dim Rg2size As Long 'number of rows in range 2
Rg2size = 21
Dim Rg2End As Long
Rg2End = Rg2Start + Rg2size 'last row of range 2

With ActiveSheet.Rows("Rg1Start:Rg1end") 
'!/!/!/!/runtime err 13 type mismatch/!/!/!/!/!/!
    .EntireRow.Hide = True
End With

With ActiveSheet.Rows("Rg2Start:Rg2End")
    .EntireRow.Hide = True
End With

End Sub
How do I fix that annoying type mismatch error?!? I don't know any other way to define ranges of entire rows. :(
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Forum statistics

Threads
1,223,099
Messages
6,170,108
Members
452,302
Latest member
TaMere

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