Combine two Subs into one

yasinv

New Member
Joined
Aug 21, 2019
Messages
1
Hi all.

I have two private subs, they work when i run them alone but i am having trouble combining them

The function of the code is id you select a value in d15 it would unhide rows depending on the value so select the number one and it hides the 14 rows below

Then move onto d16 and the axact same principal applies

First code

If Intersect(Target, Range("D15")) Is Nothing Or Target.Cells.Count > 20 Then
Exit Sub

ElseIf Range("D15").Value = "15" Then
Rows("25:39").EntireRow.Hidden = False


ElseIf Range("D15").Value = "1" Then
Rows("25").EntireRow.Hidden = False
Rows("26:39").EntireRow.Hidden = True

End If


End Sub

Second Code


if Intersect(Target, Range("D16")) Is Nothing Or Target.Cells.Count > 20 Then
Exit Sub

ElseIf Range("D15").Value = "D16" Then
Rows("41:57").EntireRow.Hidden = False


ElseIf Range("D16").Value = "1" Then
Rows("41").EntireRow.Hidden = False
Rows("42:57").EntireRow.Hidden = True


End If


End Sub

I would greatly appreciate any help
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
According to the logic of the first code, it should be like this in the second code, but check the data in red.


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'First Code
  If Target.Count > 20 Then Exit Sub
  If Not Intersect(Target, Range("D15")) Is Nothing Then
    If Range("D15").Value = "15" Then
      Rows("25:39").EntireRow.Hidden = False
    ElseIf Range("D15").Value = "1" Then
      Rows("25").EntireRow.Hidden = False
      Rows("26:39").EntireRow.Hidden = True
    End If
  End If
'Second Code
  If Not Intersect(Target, Range("D16")) Is Nothing Then
    If Range("[B][COLOR=#ff0000]D16[/COLOR][/B]").Value = "[B][COLOR=#ff0000]16[/COLOR][/B]" Then
      Rows("41:57").EntireRow.Hidden = False
    ElseIf Range("D16").Value = "1" Then
      Rows("41").EntireRow.Hidden = False
      Rows("42:57").EntireRow.Hidden = True
    End If
  End If
End Sub
 
Upvote 0
I question the validity of the statement in red font. Looks like it might be missing 'Range(? )'.

Code:
If Intersect(Target, Range("D15:D16")) Is Nothing Or Target.Cells.Count > 20 Then
    Exit Sub
[COLOR=#ff0000]ElseIf Range("D15").Value = "15" Then
[/COLOR]   Rows("25:39").EntireRow.Hidden = False

ElseIf Range("D15").Value = "1" Then
    Rows("25").EntireRow.Hidden = False
    Rows("26:39").EntireRow.Hidden = True

ElseIf Range("D15").Value = "D16" Then
    Rows("41:57").EntireRow.Hidden = False

ElseIf Range("D16").Value = "1" Then
    Rows("41").EntireRow.Hidden = False
    Rows("42:57").EntireRow.Hidden = True

End If

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

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