Two change events in same worksheet

RPM7

Board Regular
Joined
Nov 28, 2007
Messages
191
I have two macros running in a worksheet but one is functioning via a toggle button and the other is a drop down list which uses a change event.

I've made some modifications to the spreadsheet and now need a formula to detect the toggle button value.
I figured it would be best to get rid of the toggle button and clean up the worksheet with two drop down lists; however, when I do, the macro crashes.

Is there a way of fixing this?

Here's the original macros which work fine.
I just want to change the toggle button to activate based on the value of Cell L8.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    
    If Intersect(Target, Range("U13")) Is Nothing Then Exit Sub
    Select Case Target.Value
        Case "Auto Entry"
            Range("AP:AS").EntireColumn.Hidden = True
        Case "Manual Entry"
            Range("AP:AS").EntireColumn.Hidden = False
    End Select
    
    End Sub
Code:
Private Sub ToggleButton5_Click()
Worksheets("Macro Test").Activate
ActiveSheet.Unprotect ("")
Application.ScreenUpdating = False

If ToggleButton5.Value = True Then
 'This area contains the things you want to happen
'when the toggle button is not depressed

ToggleButton5.BackColor = &H8000000F
ToggleButton5.ForeColor = RGB(0, 0, 0)
ToggleButton5.Caption = "Activate Option-A

Range(Columns(18), Columns(20)).EntireColumn.Hidden = True
Range(Columns(32), Columns(41)).EntireColumn.Hidden = True
Range(Columns(15), Columns(17)).EntireColumn.Hidden = False
Range(Columns(22), Columns(31)).EntireColumn.Hidden = False

Else
'This area contains the things you want to happen
'when the toggle button is depressed


ToggleButton5.BackColor = RGB(146, 208, 80)
ToggleButton5.ForeColor = RGB(0, 0, 0)
ToggleButton5.Caption = "Activate Option-B"

Range(Columns(18), Columns(20)).EntireColumn.Hidden = False
Range(Columns(32), Columns(41)).EntireColumn.Hidden = False
Range(Columns(15), Columns(17)).EntireColumn.Hidden = True
Range(Columns(22), Columns(31)).EntireColumn.Hidden = True

ActiveSheet.Protect ("")
Application.ScreenUpdating = True

  End If
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Firstly, you can only have one Worksheet_Change event procedure per sheet. So you would have to put both in the same sub procedure (just in two blocks).
Secondly, the first line of your original Worksheet_Change event procedure exits the Sub if it not an update to cell U13, so we need to change that slightly.

So the new structure should look something like this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
'   Perform these steps for changes to U13
    If Not Intersect(Target, Range("U13")) Is Nothing Then 
         Select Case Target.Value
             Case "Auto Entry"
                 Range("AP:AS").EntireColumn.Hidden = True
             Case "Manual Entry"
                 Range("AP:AS").EntireColumn.Hidden = False
         End Select
    End If     

'   Perform these steps for changes to L8
    If Not Intersect(Target, Range("L8")) Is Nothing Then 
        'Your other code here
    End If
    
End Sub
You may need to modify your other code a bit, especially since you are not using the Toggle Button anymore.
 
Upvote 0
Thanks Joe4.

I appreciate all your help. Its a nightmare trying to decipher VBA tutorials when the problem is so simple.

I've got it all working now though. Cheers.
 
Upvote 0
You are welcome!
Glad you got it working!
 
Upvote 0

Forum statistics

Threads
1,215,563
Messages
6,125,572
Members
449,237
Latest member
Chase S

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