View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Don Guillett Don Guillett is offline
external usenet poster
 
Posts: 10,124
Default My 1st IF-THEN-ELSE Procedure

you may find this idea useful to combine

Sub hidenoncontingentrows()
Range("a1,a3,a5").EntireRow.Hidden = True
End Sub

--
Don Guillett
SalesAid Software

"Jim Cone" wrote in message
...
Hello Amy,
You are very close. The code belongs in the Sheet2 module.
It is "event" driven code.
When any cell on the sheet is changed the code will run.
However, the code checks if cell A1 is the changed cell
(the target cell) and if not the code exits...
'---------------------------------------------
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$A$1" Then
If Target.Text = "Apples" Then
Rows("10:10").EntireRow.Hidden = True
Rows("20:20").EntireRow.Hidden = True
ElseIf Target.Text = "Pears" Then
Rows("15:15").EntireRow.Hidden = True
Rows("25:25").EntireRow.Hidden = True
ElseIf Target.Text = "Oranges" Then
Rows("30:30").EntireRow.Hidden = True
Rows("35:35").EntireRow.Hidden = True
Else
Rows("1:35").EntireRow.Hidden = False
Rows("12:12").EntireRow.Hidden = True
Rows("16:16").EntireRow.Hidden = True
End If
End If

End Sub

--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware


"AMY Z."
wrote in message
Hi,
This is what I'm trying to do: It is in Worksheet2. I would like it to run
when cell A1 changes:
If cell A1 says Apples then hide rows 10 & 20.
If A1 says Pears then hide rows 15 & 25.
If A1 says Oranges then hide rows 30 & 35.
If A1 doesn't say any of the above then Unhide all rows (to reset) and
hide
rows 12 & 16. (This is Default for all other Fruits).
This is what I wrote but it won't work. I'm not getting any code errors,
but
it just sits there.

If Worksheets("Sheet2").Range("A1").Text = "Apples" Then
Rows("10:10").Select
Selection.EntireRow.Hidden = True
Rows("20:20").Select
Selection.EntireRow.Hidden = True
ElseIf Worksheets("Sheet2").Range("A1").Text = "Pears" Then
Rows("15:15").Select
Selection.EntireRow.Hidden = True
Rows("25:25").Select
Selection.EntireRow.Hidden = True
ElseIf Worksheets("Sheet2").Range("A1").Text = "Oranges" Then
Rows("30:30").Select
Selection.EntireRow.Hidden = True
Rows("35:35").Select
Selection.EntireRow.Hidden = True
Else: Worksheets ("Sheet2")
Rows("1:35").Select
Selection.EntireRow.Hidden = False
Rows("12:12").Select
Selection.EntireRow.Hidden = True
Rows("16:16").Select
Selection.EntireRow.Hidden = True
End If

End Sub

Should I put this under the General section of the Sheet2 Object in the
VB
editor?
This is the first procedure that I've tried to write.
Thank for your time,
Amy