*** Worksheet_Change Error - Combining code in the same worksheet ! ***

Zwen

New Member
Joined
Jul 22, 2011
Messages
8
Hi, Am trying to combine two Worksheet_Change commands in the same worksheet but keep getting a Worksheet_Change error message. The codes are below and they are each running off a separate combo boxes located on the same work sheet performing separate functions on two alternative worksheets. I'm aware that they can be combined but so far have not been able to figure out how to do this correctly. Any help would be appreciated. Tx

1#

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$I$24" Then
Sheets("Inputs").Scenarios(Target.Value).Show

End If

End Sub



2#

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$I$29" Then
Sheets("DCF_Analysis").Scenarios(Target.Value).Show

End If

End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
One way:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Select Case Target.Address(False, False)
        Case "I24"
            Worksheets("Inputs").Scenarios(Target.Value).Show
        Case "I29"
            Worksheets("DCF_Analysis").Scenarios(Target.Value).Show
    End Select
End Sub
 
Upvote 0
Tx for the help. Unfortunately the second code doesn't work when I use that formulation. The first works perfectly though...
 
Upvote 0
That code replaces both of yours.

It assumes that the changing cells (I24 and I29) are both on the same sheet, and that the cells change via manual entry (or other code), not as the result of formulas.
 
Last edited:
Upvote 0
I do understand that the code you supplied replaces both of mine and have done so but as per my last the first code works but the second doesn't. Perhaps a bit more detail would be of use.

I'm trying to run two combo (drop downs) boxes located on the same work sheet which drive scenarios each respectively drive scenarios on two other worksheets (i.e. not on the same worksheet that they are located on). I included the first code (1#) on the worksheet several weeks ago and am now trying to add the second (2#) as I have added another combo box to the first.

When I run the second combo box I get a 'debugging error' and the line: 'Worksheets("DCF_Analysis").Scenarios(Target.Value).Show' is highlighted in yellow. When hovered above it states that "Target.Value="GGM" one of the two options for the particular combo box associated with that code.

Hope thats clarified. Tx again.
 
Upvote 0
VBA - Worksheet_Change Error# - How to combine two codes in the same worksheet

Hi, Am trying to combine two Worksheet_Change commands in the same worksheet but keep getting a Worksheet_Change error message when I try and select one of the drop down options.

The codes are below. They are each running off separate combo "drop down" boxes located on a work sheet entitled 'Assumptions' and Code 1# (Combo Box 1#) affects a change in a worksheet entitled "Inputs and Code 2# (Combo Box 2#) affects a change in a worksheet entitled "DCF Analysis". Each combo box is driven by a 'Scenario Manager' (located on the respective work sheets on which the changes are being affected namely "Inputs" and "DCF Analysis").

Code 1 has worked fine for the last two weeks until I tried to add the second combo box to the worksheet and add the associated code.

I'm aware that the codes can be combined but so far have not been able to figure out how to do this correctly. Any help would be appreciated. Tx

1#

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$I$24" Then
Sheets("Inputs").Scenarios(Target.Value).Show

End If

End Sub



2#

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$I$29" Then
Sheets("DCF_Analysis").Scenarios(Target.Value).Show

End If

End Sub
 
Upvote 0
Re: VBA - Worksheet_Change Error# - How to combine two codes in the same worksheet

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$I$24" Then
Sheets("Inputs").Scenarios(Target.Value).Show
elseif If Target.Address = "$I$29" Then
Sheets("DCF_Analysis").Scenarios(Target.Value).Show
End If
End Sub
 
Upvote 0
Upvote 0
Since there were replies to both threads, I have merged them together (posts 6,7, and 8 were from new thread).
 
Upvote 0
Hi Tom, Many thanks for the help. The code you gave me is generating an error for both combo boxes as below. Any thoughts?

'Compile error:

Syntax error.'
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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