Worksheet_Change (adding two target addresses into one VBA code

VonFeed

New Member
Joined
Dec 12, 2011
Messages
11
Hello, I am trying to have a Worksheet_Change that will run a VBA code. Right now I have the first part done. What this code does is that it works with a Drop-down list in Cell V6 and when someone selects this drop-down list it will run a code called Do_it_1 and also change a commandbutton caption. This works great so far.

What I would like to do is, make this same code work for cell $V$2, which is also a drop-down list. When I tried to have them two separate items I would get a compile error: Ambiguous name detected:worksheet_change.

Here is my code:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Address <> "$V$6" Then Exit Sub
Application.Run "Do_it_1"
Select Case CommandButton1.Caption
Case "Hide Object Detail"
CommandButton1.Caption = "Expand Objects"
End Select
End Sub


Is there a way to add my second target address into my code? Also, I have not thought of this yet, will this only effect the active worksheet and not other worksheets in the same workbook? I would rather keep this to the active sheet only. Thank you for your help.
 
Last edited by a moderator:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("V2,V6")) Is Nothing Then Exit Sub
    If Target.Count > 1 Then Exit Sub
    Application.Run "Do_it_1"
    Select Case CommandButton1.Caption
        Case "Hide Object Detail"
            CommandButton1.Caption = "Expand Objects"
    End Select
End Sub
 
Upvote 0
You cannot have two "Worksheet_Change" event procedures on the same sheet.
For that matter, you cannot have two procedures with the same name in the same module.

EDIT: I slightly misread your original question. The code below shows how to structure if you did NOT want to do the exact same thing for both ranges.


You just need to have multiple code blocks in the one procedure, like this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)


    If Target.Count > 1 Then Exit Sub

'   Block1    
    If Target.Address = "$V$6" Then
        Application.Run "Do_it_1"
        Select Case CommandButton1.Caption
            Case "Hide Object Detail"
            CommandButton1.Caption = "Expand Objects"
        End Select
    End If
    
'   Block2
    If Target.Address = "$V$2" Then
        'DO STUFF HERE
    End If


End Sub
 
Last edited:
Upvote 0
Wow, that was quick. It works perfectly. The first fix is what I needed, but seeing the second one might help me in any future endeavors. Thank you for your help.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,260
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