Sub separation issues

OversizedCranium

New Member
Joined
Aug 18, 2015
Messages
26
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am very new to VBA so this is probably a quick fix, but I can't seem to get a second command to work underneath the first....

Is anyone able to help?

The first IF works perfectly, however I'm struggling to add another below it.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Activate
If Not Application.Intersect(Range("C18"), Range(Target.Address)) Is Nothing Then
    Select Case Target.Value
    Case Is = "Residential": Rows("33:45").EntireRow.Hidden = True
                             Rows("19:32").EntireRow.Hidden = False
    Case Is = "Education": Rows("19:32").EntireRow.Hidden = True
                           Rows("33:37").EntireRow.Hidden = False
                           Rows("38:45").EntireRow.Hidden = True
    Case Is = "Res & Ed": Rows("19:37").EntireRow.Hidden = False
                          Rows("38:45").EntireRow.Hidden = True
    Case Is = "Part-Time": Rows("19:37").EntireRow.Hidden = True
                           Rows("38:45").EntireRow.Hidden = False
    Case Is = "None Required": Rows("19:45").EntireRow.Hidden = True
    
    End Select
    End If
    
  
End Sub

Private Sub Worksheet_Change_Bel(ByVal Target As Range)
ActiveSheet.Activate
If Not Application.Intersect(Range("C52"), Range(Target.Address)) Is Nothing Then
    Select Case Target.Value
    Case Is = "No": Rows("53:55").EntireRow.Hidden = True
    Case Is = "Yes": Rows("53:55").EntireRow.Hidden = False
    
    End Select
    End If
    
    End Sub

Thanks
 

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.
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(0, 0) = "C18" Then
    Select Case Target.Value
    Case Is = "Residential": Rows("33:45").EntireRow.Hidden = True
                             Rows("19:32").EntireRow.Hidden = False
    Case Is = "Education": Rows("19:32").EntireRow.Hidden = True
                           Rows("33:37").EntireRow.Hidden = False
                           Rows("38:45").EntireRow.Hidden = True
    Case Is = "Res & Ed": Rows("19:37").EntireRow.Hidden = False
                          Rows("38:45").EntireRow.Hidden = True
    Case Is = "Part-Time": Rows("19:37").EntireRow.Hidden = True
                           Rows("38:45").EntireRow.Hidden = False
    Case Is = "None Required": Rows("19:45").EntireRow.Hidden = True
    
    End Select
ElseIf Target.Address(0, 0) = "C52" Then
    Select Case Target.Value
    Case Is = "No": Rows("53:55").EntireRow.Hidden = True
    Case Is = "Yes": Rows("53:55").EntireRow.Hidden = False
    
    End Select
    
    End If
    
  
End Sub
 
Upvote 0
Everything must be inside of a single "Worksheet_Change" event procedure, and you cannot take any liberties in renaming it if you want it to work automatically.

Also,
Range(Target.Address)
is redundant and totally unnecessary. "Target" is already a range variable.

Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Range("C18"), Target) Is Nothing Then
    Select Case Target.Value
        Case Is = "Residential": Rows("33:45").EntireRow.Hidden = True
                                 Rows("19:32").EntireRow.Hidden = False
        Case Is = "Education": Rows("19:32").EntireRow.Hidden = True
                               Rows("33:37").EntireRow.Hidden = False
                               Rows("38:45").EntireRow.Hidden = True
        Case Is = "Res & Ed": Rows("19:37").EntireRow.Hidden = False
                              Rows("38:45").EntireRow.Hidden = True
        Case Is = "Part-Time": Rows("19:37").EntireRow.Hidden = True
                               Rows("38:45").EntireRow.Hidden = False
        Case Is = "None Required": Rows("19:45").EntireRow.Hidden = True
    End Select
End If
    
If Not Application.Intersect(Range("C52"), Target) Is Nothing Then
    Select Case Target.Value
        Case Is = "No": Rows("53:55").EntireRow.Hidden = True
        Case Is = "Yes": Rows("53:55").EntireRow.Hidden = False
    End Select
End If
    
End Sub
 
Upvote 0
Awesome,

Works perfect,

going forward with this logic, would I apply Elseif again for further adjustments?
 
Upvote 0
In what way?
In where the wording is aligned?

Sorry if that's a stupid question, but I tried to replicate it for another parameter, didn't seem to work, copied yours and changed it to fit below and it suddenly worked
 
Upvote 0
You don't need to format the code with indents, but I always do as I find it easier to see the general flow of the code.
 
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,946
Members
449,480
Latest member
yesitisasport

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