excel selectionChange导致两个子例程都运行

lhcgjxsq  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(86)

我有

' When Selection is Changed
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Rem call the - Sort_Customers_Countries - Subroutine & call the - Go_First_Sheet - Subroutine
If Not Intersect(Target, Me.Range("cel__Sheets_Formula")) Is Nothing Then
    Sort_Customers_Countries
    Go_First_Sheet
End If

Rem call the - Center_Resize_Image - Subroutine
If Not Intersect(Target, Me.Range("col__Customer_Country_Flag")) Is Nothing Then
    Center_Resize_Image
End If

End Sub

字符串
问题是,当选择cel__Sheets_Formula时,它会同时运行Sort_Customers_CountriesGo_First_Sheet以及Center_Resize_Image
我怀疑它与Target有关。cel__Sheets_Formula是一个命名的单元格,col__Customer_Country_Flag是一个表中命名的列。
命名范围不相交。cel__Sheets_Formula='Customers Countries'!$F$1col__Customer_Country_Flag=tbl__Customers_Countries[Customer Country Flag]

azpvetkf

azpvetkf1#

编辑

看来解决办法是

' When Selection is Changed
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim currentCell As Range
Set currentCell = ActiveCell

Rem call the - Sort_Customers_Countries - Subroutine & call the - Go_First_Sheet - Subroutine
If Not Intersect(Target, Me.Range("cel__Sheets_Formula")) Is Nothing Then
    Sort_Customers_Countries
    Go_First_Sheet
End If

Rem call the - Center_Resize_Image - Subroutine
If Not Intersect(currentCell, Me.Range("col__Customer_Country_Flag")) Is Nothing Then ' ///// instead of Target
    Center_Resize_Image
End If

End Sub

字符串

相关问题