PDA

View Full Version : VBA Help - Newbie



stockjc
08-28-2012, 03:00 AM
Hi,

Im looking for some assitance in writing some code. I am very new at this so sorry if my question seems basic and easy but I have no idea where to start. Basically I am looking at writing code which does the following:

If A1 = 1 then hide rows 3:10, 11, 15, 19, 20:50 etc
If A1 = 2 then hide rows 3:10, 15, 29:52, 82
If A1 = 3 then hide rows 3:20, 95
If A1 = 4 then hide rows 8:10, 12, 14, 16, 18, 20, 22, 24:60

Now im able to get the first code which I have written as:

Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A1") = 1 Then
Rows("3:10").EntireRow.Hidden = True
Rows("11").EntireRow.Hidden = True
Rows("15").EntireRow.Hidden = True
Rows("19").EntireRow.Hidden = True
Rows("20:50").EntireRow.Hidden = True

End If

End Sub

But as soon as I write the same again chaning 1 to 2, it seems to contridict itself and does nothing. Any help would be greatly appreciated as I really dont know what im doing lol.

Thanks

James

Bob Phillips
08-28-2012, 03:14 AM
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Range("A1").Value

Case 1: Me.Range("3:10,11:11,15:15,19:19,20:50").EntireRow.Hidden = True
Case 2: Me.Range("3:10,15:15,29:52,82:82").EntireRow.Hidden = True
Case 3: Me.Range("3:20,95:95").EntireRow.Hidden = True
Case 4: Me.Range("8:10,12:12,14:14,16:16,18:18,20:20,22:22,24:60").EntireRow.Hidden = True
End Select
End Sub

Aussiebear
08-28-2012, 02:34 PM
Bob, Could the Case1 line be rewritten as
Case 1: Me.Range("3:11,15:15,19:50").EntireRow.Hidden = True

Bob Phillips
08-28-2012, 03:05 PM
Bob, Could the Case1 line be rewritten as
Case 1: Me.Range("3:11,15:15,19:50").EntireRow.Hidden = True

LOL, of course it could, didn't spot that :doh:

Teeroy
08-28-2012, 05:10 PM
Since the rows hidden by the cases overlap you may want to unhide all at the beginning of the procedure (such as "Me.Rows.Hidden = False") as well otherwise hiding the rows will be cumulative.