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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
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,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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