Multiple Sub Worksheet_Change(ByVal Target As Range)

Nick70

Active Member
Joined
Aug 20, 2013
Messages
299
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a macro Sub Worksheet_Change(ByVal Target As Range) which runs when cell A4 is changed see code below:

VBA Code:
Sub worksheet_change(ByVal target As Range)
Set target = Range("A4")
If target.Value = "Escalation Form" Then
 Call Expand
End If
If target.Value = "Relationship Profile Summary Form" Then
Call Collapse
End If
End Sub

I would now like to add another condition so that if cell B8 changes then some rows are hidden.

Code would look like one below:

Code:
Sub worksheet_change(ByVal target As Range)
Set target = Range("B8")
If target.Value = "Counterparty" Then
Call Hide
End If
If target.Value = "Client" Then
Call Unhide
Call Paste
End If
If target.Value = "Business Partner" Then
Call Unhide
Call Paste
End If
End Sub

How can I combine these two pieces of code?

If I add second one I get Compile error: "Ambiguous name detected: worksheet_change"

Many Thanks,
Nic
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.Address(0, 0) = "A4" Then
      If Target.Value = "Escalation Form" Then
         Call Expand
      ElseIf Target.Value = "Relationship Profile Summary Form" Then
         Call Collapse
      End If
   ElseIf Target.Address(0, 0) = "B8" Then
      If Target.Value = "Counterparty" Then
         Call Hide
      ElseIf Target.Value = "Client" Then
         Call Unhide
         Call Paste
      ElseIf Target.Value = "Business Partner" Then
         Call Unhide
         Call Paste
      End If
   End If
End Sub
 
Upvote 0
Try something like this
VBA Code:
Sub Worksheet_change(ByVal Target As Range)
    Dim T As String:    T = Target.Value
    If Target.Address(0, 0) = "A4" Then
        Select Case T
            Case "Escalation Form"
                Call Expand
            Case "Relationship Profile Summary Form"
                Call Collapse
        End Select
    ElseIf Target.Address(0, 0) = "B8" Then
        Select Case T
            Case "Counterparty"
                Call Hide
            Case "Client", "Business Partner"
                Call Unhide
                Call Paste
        End Select
    End If
End Sub
 
Last edited:
Upvote 0
I think this should also work
VBA Code:
Sub Worksheet_Change(ByVal Target As Range)
    Select Case Target.Address(0, 0) & Target.Value
        Case "A4Escalation Form"
            Call Expand
        Case "A4Relationship Profile Summary Form"
            Call Collapse
        Case "B8Counterparty"
            Call Hide
        Case "B8Client", "B8Business Partner"
            Call Collapse
            Call Paste
    End Select
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,698
Members
448,979
Latest member
DET4492

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