Running two codes - Private Sub Worksheet_Change(ByVal Target As Range)

countryfan_nt

Well-known Member
Joined
May 19, 2004
Messages
758
Hello friends, hope all is well.
the below code works smoothly, but I want to add another action.
I want both codes to be placed in the same sheet module, and to take actions simultaneously please:

thank you so much in advance!

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("S16")) Is Nothing Then Exit Sub
 
With Target
    If Range("s16").Value = "w/o Ins" Then
    
    Range("18:18,30:30").Select
    Selection.EntireRow.Hidden = True
    Range("A1").Select
   
    End If

    If Range("s16").Value <> "w/o Ins" Then
    Range("18:18,30:30").Select
    Selection.EntireRow.Hidden = False
    Range("A1").Select

End If
End With
End Sub

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("R16")) Is Nothing Then Exit Sub
 
With Target
    If Range("R16").Value = "Dubai" Then
    
    Range("17:17,29:29").Select
    Selection.EntireRow.Hidden = False
    Range("A1").Select
   
    End If

    If Range("R16").Value <> "Dubai" Then
    Range("17:17,29:29").Select
    Selection.EntireRow.Hidden = True
    Range("A1").Select

End If
End With
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
As a rule, you can only have a single Worksheet event per worksheet. However, there is a work-around for it. In order to run more than one event, you must Call each from within the Worksheet event. The example below should give you an idea on how to accomplish this.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
	Call Macro1(Target)
	Call Macro2(Target)
End Sub

Sub Macro1(ByVal Target As Range)
	Do MyTask1
End Sub

Sub Macro2(ByVal Target As Range)
	Do MyTask2
End Sub

So yours would look something like

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
	Call Macro1(Target)
	Call Macro2(Target)
End Sub

Sub Macro1(ByVal Target As Range)
If Intersect(Target, Range("S16")) Is Nothing Then Exit Sub
 
With Target
    If Range("s16").Value = "w/o Ins" Then
    
    Range("18:18,30:30").Select
    Selection.EntireRow.Hidden = True
    Range("A1").Select
   
    End If

    If Range("s16").Value <> "w/o Ins" Then
    Range("18:18,30:30").Select
    Selection.EntireRow.Hidden = False
    Range("A1").Select

End If
End With
End Sub

Sub Macro2(ByVal Target As Range)
If Intersect(Target, Range("R16")) Is Nothing Then Exit Sub
 
With Target
    If Range("R16").Value = "Dubai" Then
    
    Range("17:17,29:29").Select
    Selection.EntireRow.Hidden = False
    Range("A1").Select
   
    End If

    If Range("R16").Value <> "Dubai" Then
    Range("17:17,29:29").Select
    Selection.EntireRow.Hidden = True
    Range("A1").Select

End If
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,389
Messages
6,124,662
Members
449,178
Latest member
Emilou

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