Excel 如何设置更改后运行多个不同宏的多个范围?
我的问题很简单,但出于某种原因,我不知道怎么做。问题是我有16个不同的范围和16个不同的宏 命名范围为:Excel 如何设置更改后运行多个不同宏的多个范围?,excel,vba,excel-formula,Excel,Vba,Excel Formula,我的问题很简单,但出于某种原因,我不知道怎么做。问题是我有16个不同的范围和16个不同的宏 命名范围为: GManA GVisA GManB GVisB 它一直持续到GManH和GVisH 我想要运行的宏非常相似。问题是:每当我对GManA或GVisA进行任何更改时,我都会运行宏AttResA和CpGrlA。到目前为止,代码中的所有内容都是正确的,但我认为每个人都看到了我的意图。我想对此进行扩展:每当我对GManB或GVisB进行更改时,它都应该运行宏AttResB和CpGrlB 我现在的代
- GManA
- GVisA
- GManB
- GVisB
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("GManA,GVisA")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
AttResA
CpGrlA
End If
End Sub
我该如何扩展它呢?我想这真的取决于你的其他潜艇做什么。我建议更改Sub以接收更改的单元格
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Debug.Print "Target: " & Target.Address
Set KeyCells = Intersect(Target, Range("GManA,GVisA"))
If Not KeyCells Is Nothing Then
AttResA KeyCells
CpGrlA KeyCells
Else
Set KeyCells = Intersect(Target, Range("GManB,GVisB"))
If Not KeyCells Is Nothing Then
AttResB KeyCells
CpGrlB KeyCells
End If
End If
Set KeyCells = Nothing
End Sub
' Normal Module
Sub AttResA(ByRef Target As Range)
Debug.Print "AttResA()", Target.Address
End Sub
Sub CpGrlA(ByRef Target As Range)
Debug.Print "CpGrlA()", Target.Address
End Sub
Sub AttResB(ByRef Target As Range)
Debug.Print "AttResB()", Target.Address
End Sub
Sub CpGrlA(ByRef Target As Range)
Debug.Print "CpGrlB()", Target.Address
End Sub
您可以使用
应用程序。运行并在特定范围内循环,但我不得不质疑为什么需要16个不同的子例程。我猜测(可能是错误的)首先要在这方面进行改进。