Call Module Subroutine from SelectionChange Event

Caleeco

Well-known Member
Joined
Jan 9, 2016
Messages
980
Office Version
  1. 2010
Platform
  1. Windows
Hello,

I'm having a code structure issue, so the code below is just representative of the problem.

I have several sheets which I would like to have a SelectionChange Event trigger some code to run. There is one routine that I have that needs to run regardless of which SelectionChange event it triggered, so I created a module with said code in the hopes I could CALL it from each of the Sheet codes.

Worksheet Sheet1 Code
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Call ColourCell
End Sub


Module Level Sub-Routine i want to call
Code:
Public Sub ColourCell()
    ActiveCell.Interior.Color = vbYellow
End Sub

However, upon triggering the SelectionChange event, i get a Compile Error saying that "Sub or Function not defined".

The only way around this is copying the ColourCell() routine into each of the sheet codes, which doesn't feel like the most efficient solution.

Any help is appreciated.
Cheers

Caleeco
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Put the following code in the events of thisworkbook

Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Call ColourCell
End Sub

Put the following in a module

Code:
Public Sub ColourCell()
    ActiveCell.Interior.Color = vbYellow
End Sub

It works for me. Select any cell on any page and color it
 
Upvote 0
Hi Dante,

Thanks for that solution. I didnt know you could have event code in 'ThisWorkbook'. It does work for this example, however, the code I currently have is much more complex.

I also have IF NOT INTERSECT method to only trigger code on certain selections (the range differers for each sheet).

I tried the below, which works for selections on Sheet1. However, if i Select anything on Sheet 2. I get a Method 'Intersect' of Object '_Global' failed error

Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    If Intersect(ActiveCell, Sheet1.Range("A1:A10")) Is Nothing Then
        MsgBox "The active cell does NOT Intersect A1:A10"
        Call ColourCell
    End If
End Sub

Thanks for your assistance
Caleeco
 
Upvote 0
Forget the macros a little. Better explain what you need to do on each sheet.
 
Upvote 0
Caleeco

You should be using Sh and Target in the code, they are references to the sheet the selection has been changed on and the range that has been selected.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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