VBA - multiple filters from clicking different cells

jh9940

New Member
Joined
Jul 18, 2024
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi

I successfully used the following code:

Option Explicit

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


I have multiple macros that filter data (school grades) e.g., Key Stage 3 - Academic Review 1. My problem is that whenever I add the same code again for another macro it doesn't work and I get the error message: Compile error: Ambiguous name detected: worksheet_selectionChange.

I've now learnt you can't have two of the same and I am not skilled enough to be able to write multiple macros within the same bit of code.

How can I have multiple macros assigned to different cells please?

Huge thanks in advance

JH
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Welcome to the Board!

Just repeat this section in your code, replacing the range address and the procedure you want to call each time:
Rich (BB code):
If Not Intersect(Target, Range("I2")) Is Nothing Then
    Call AR1_KS3
End If
 
Upvote 0
Thank you so much for your reply. I tried this code:

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("I2")) Is Nothing Then
Call AR1_KS3
If Not Intersect(Target, Range("E2")) Is Nothing Then
Call clear_filters

End If
End If
End Sub

I added the bit in bold but I get the following error message: "Compile error: Block If without End If". Sorry to be a pain!

Many thanks
 
Upvote 0
You have three IFs, but only two END IFs.

In any event, I don't think your code will work. You do NOT want to imbed the IFs inside one another. They should be totally separate.
I also changed your first IF to be self-contained and simply exit the sub if that first condition is not met (the one checking how many cells were updated).

So try this. If you need to add more, follow the same pattern ("Third Check", etc).
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Selection.CountLarge  > 1 Then Exit Sub

'   First Check
    If Not Intersect(Target, Range("I2")) Is Nothing Then
        Call AR1_KS3
    End If

'   Second Check
    If Not Intersect(Target, Range("E2")) Is Nothing Then
        Call clear_filters
    End If

End Sub
 
Upvote 0
Solution
Bloody brilliant!! Thank you for solving this - amazing.

To be super cheeky - I have five identical sheets (Year 7, Year 8, Year 9, Year 10, Year 11). Is there anyway to apply the above code to all the sheets? i.e., it doesn't matter if I click E2 on the Year 7 or Year 9 sheet it will still run the same macro.

Do you have a 'buy me a coffee' link so that I can get one for you?

Thanks again
 
Upvote 0
I know you can do it for a change value across all sheets in a workbook, but I do not see a way to do it for cell selections.
(I am not saying with 100% certainty it is not possible, just saying that I do not see a way to do it).

At worst, you would just need to copy that code in to the Sheet modules for the 5 sheets you want to apply it to.
 
Upvote 0
I know you can do it for a change value across all sheets in a workbook, but I do not see a way to do it for cell selections.
(I am not saying with 100% certainty it is not possible, just saying that I do not see a way to do it).

At worst, you would just need to copy that code in to the Sheet modules for the 5 sheets you want to apply it to.
Actually, I misspoke. There is a way to do it in just one procedure!
In the "ThisWorkbook" module, there is a event procedure called "SheetSelectionChange". If you put the code in there, it will apply to ALL sheets in your workbook.

So you could use that. Just bear in mind that it there are sheets you do NOT want to apply it to, you will need to add coding in there to check the sheet name first.
 
Upvote 0

Forum statistics

Threads
1,223,887
Messages
6,175,199
Members
452,617
Latest member
Narendra Babu D

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