VBA to run various macros when clicking on different cells in Excel

Joe Smith 84

New Member
Joined
Feb 5, 2023
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Good day

I've come across VBA code that works really well to trigger a macro in Excel when clicking on a specific cell. See below:


Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("D4")) Is Nothing Then
Call MyMacro
End If
End If
End Sub


I'm trying to add 3 more such scenarios to the same code, but I have no experience with coding, so troubleshooting has gotten me nowhere. Please see below example of what I'm trying to do, and correct the code if possible:


Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("D4")) Is Nothing Then
Call MyMacro1
End If
End If
If Selection.Count = 1 Then
If Not Intersect(Target, Range("E10")) Is Nothing Then
Call MyMacro2
End If
End If
If Selection.Count = 1 Then
If Not Intersect(Target, Range("G23")) Is Nothing Then
Call MyMacro3
End If
End If
If Selection.Count = 1 Then
If Not Intersect(Target, Range("J33")) Is Nothing Then
Call MyMacro4
End If
End If
End Sub


I was expecting the various macros I created (that work well) to automatically run when the cells noted in the code were clicked on


Any assistance will be greatly appreciated!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
There are no errors in your macro; it only has redundant code. No need to repeat the check Selection.Count = 1 . You can easily reduce it to this:
VBA Code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("D4")) Is Nothing Then Call myMacro1
        If Not Intersect(Target, Range("E10")) Is Nothing Then Call myMacro2
        If Not Intersect(Target, Range("G23")) Is Nothing Then Call myMacro3
        If Not Intersect(Target, Range("J33")) Is Nothing Then Call myMacro4
    End If
End Sub
 
Last edited:
Upvote 0
Welcome to the MrExcel board!

Try this structure instead

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Selection.CountLarge = 1 Then
    Select Case Target.Address(0, 0)
      Case "D4": Call MyMacro1
      Case "E10": Call MyMacro2
      Case "G23": Call MyMacro3
      Case "J33": Call MyMacro4
      Case Else: 'Do nothing
    End Select
  End If
End Sub
 
Upvote 0
There are no errors in your macro; it only has redundant code. No need to repeat the check Selection.Count = 1 . You can easily reduce it to this:
VBA Code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("D4")) Is Nothing Then Call myMacro1
        If Not Intersect(Target, Range("E10")) Is Nothing Then Call myMacro2
        If Not Intersect(Target, Range("G23")) Is Nothing Then Call myMacro3
        If Not Intersect(Target, Range("J33")) Is Nothing Then Call myMacro4
    End If
End Sub
Thanks for the assistance. I get the following error when clicking on any of the cells in my code:

1675641340497.png


Any suggestions?
 
Upvote 0
Welcome to the MrExcel board!

Try this structure instead

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Selection.CountLarge = 1 Then
    Select Case Target.Address(0, 0)
      Case "D4": Call MyMacro1
      Case "E10": Call MyMacro2
      Case "G23": Call MyMacro3
      Case "J33": Call MyMacro4
      Case Else: 'Do nothing
    End Select
  End If
End Sub
Thanks for the assistance. I get the following error when clicking on any of the cells in my code:

1675641460297.png


Any suggestions?
 
Upvote 0
Both codes worked when I tested them.
What line of code in each one is highlighted when you click Debug?
 
Upvote 0
Both codes worked when I tested them.
What line of code in each one is highlighted when you click Debug?
The excel file closes and doesn't show any debug screen. When I view the code and select "Toggle" - "Breakpoint", it shows the following:

1675642745906.png


Is there something in the actual sheet I should look at? Or can I tweak the code a bit?

Apologies, VBA code is really new to me and I appreciate this forum and your assistance A LOT!
 
Upvote 0
@Joe Smith 84, your issue is in your 'called' macros, not our code.
Thanks rollis13. The macros run fine when I select them, or even when I use the shortcuts I created. The names are quite long though, do you suggest I maybe rename them to something shorter?

Also, the cells I'm electing to be selected are populated with text, which guides the user on where to click to unhide certain rows, might that be something that's causing the issue?
 
Upvote 0
Toggle breakpoint is not relevant at the moment.
When your tried the code you showed this error.

1675643085074.png


That message has a Debug button. Are you saying that Excel closed when you clicked that button? Instead, it should take you to the code and highlight yellow the line that caused the error.
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

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