I have a test Macro
Sub test()
Dim rSrcMatrix As Range
Set rSrcMatrix = Sheets("Code Matrix").Range("Xfer_To_Xfer_Matrix").Range("A1")
Set rSrcMatrix = rSrcMatrix.Resize(rSrcMatrix.SpecialCells(xlCellTypeLastCell).Row, rSrcMatrix.SpecialCells(xlCellTypeLastCell).Column)
End Sub
I am using this macro to test my COM addin that I have created in VS2010. I have delegated the SheetSelectionChange
event in the addin to some function.
Now I notice that whenever I run this macro, Excel fires the SheetSelectionChange
event 4 times and my addin calls the associated method for that many times.
Is there anything that I am missing or is this a bug in excel?
I believe and I could be wrong because I couldn't find an MSDN article to prove it but SpecialCells performs a type of selection and triggers the
Worksheet_SelectionChange
or theWorkbook_SheetSelectionChange
event and hence you need to switch off events.Here is a simple way to test it.
Place this code in the Sheet Code Area
Worksheet_SelectionChange
andWorkbook_SheetSelectionChange
do the same job.Worksheet_SelectionChange
is used in the sheet code are for a specific sheet. AndWorkbook_SheetSelectionChange
is used when you want the event to fire across all the sheets in that workbook.Now, we have two alternatives. Based on your above question we cannot use
Alternative One
. So you may directly skip toAlternative 2
ALTERNATIVE 1
Switch Off Events
ALTERNATIVE 2
Instead of using
SpecialCells
to find the last row or the last column, we will use.Find
.