Using a variable to identify a row

B

Brian Belliveau

Following up to my earlier post (Macro to unhide selected columns and rows)
....

Given this line to unhide 2 specific rows ...

Worksheets("PayPeriod_01").Rows("50:51").Hidden = False

How do I use variables to represent ... Rows("50:51")

I've tried ...

Dim MyRange As Range, r1 As Range, r2 As Range, r3 As Range, r4 As Range
Set MyRange = Worksheets("Employees").Range("D3:D22")
For c = 3 To 22
If MyRange.Cells(c, 1).Value = "X" Then
Set r1 = 44 + (2 * c)
Set r2 = 44 + (2 * c) + 1
Set r3 = 294 + (2 * c)
Set r4 = 294 + (2 * c) + 1
Worksheets("PayPeriod_01").Rows(r1, r2).Hidden = False
Worksheets("PayPeriod_01").Rows(r3, r4).Hidden = False

I'm sure my syntax is incorrect, but I just simply can't figure out the
correct way.

( Is it just me, or is "help" a terrible name for what happens when you
press F1?)

Thanks
 
H

Harald Staff

Hi

One way is to imitate a string:

Sub test()
Dim L1 As Long, L2 As Long
L1 = 14
L2 = 18
Rows(L1 & ":" & L2).Select
End Sub
 
B

Brian Belliveau

Thanks , but ...

I am interpolating your code to my situation without success ...

My interpretation (in my actual code) is ...

Sub RowUnhide()
Dim MyRange As Range
Dim r1 As Long, r2 As Long, r3 As Long, r4 As Long
Set MyRange = Worksheets("Employees").Range("D3:D22")
For c = 3 To 22
If MyRange.Cells(c, 1).Value = "X" Then
Set r1 = (44 + (2 * c))
Set r2 = 44 + (2 * c) + 1
Set r3 = 294 + (2 * c)
Set r4 = 294 + (2 * c) + 1
Worksheets("PayPeriod_01").Rows(r1 & ":" & r2).Hidden = False
Worksheets("PayPeriod_01").Rows(r3 & ":" & r4).Hidden = False
End If
Next
End Sub

Any help with this?

Thanks
 
D

Dave Peterson

Remove those "set's" from lines like:

Set r1 = (44 + (2 * c))
(just use)
r1 = (44 + (2 * c))

If that's not the only thing that's causing the problem, post back with some
more details.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top