Excel 组合使用私有子工作表的两个VBA术语_Change(ByVal目标作为范围)
我有这个VBA代码Excel 组合使用私有子工作表的两个VBA术语_Change(ByVal目标作为范围),excel,vba,Excel,Vba,我有这个VBA代码 Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = False If Not Intersect(Target, Range("rngBarcodeInput")) Is Nothing Then If Application.CutCopyMode = xlCopy Then Application.Undo
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("rngBarcodeInput")) Is Nothing Then
If Application.CutCopyMode = xlCopy Then
Application.Undo
Target.PasteSpecial Paste:=xlPasteValues
End If
Range("DJ5").Copy
Range("rngBarcodeInput").PasteSpecial Paste:=xlPasteFormats
Range("rngShipCheckInputFieldsNoBarcode").ClearContents
Range("rngEditStatus").ClearContents
End If
Application.EnableEvents = True
End Sub
我想将下面的代码添加到VBA中,但仅当我删除上面的代码时才起作用,因为它们都使用工作表更改。所有合并成一个私人子系统的组合都不起作用
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("C7")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
Range("C15").Value = Range("B15").Value
End Sub
我认为这是可行的,假设您不希望C15值的变化导致另一个事件触发
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Application.EnableEvents = False
If Not Intersect(Target, Range("rngBarcodeInput")) Is Nothing Then
If Application.CutCopyMode = xlCopy Then
Application.Undo
Target.PasteSpecial Paste:=xlPasteValues
End If
Range("DJ5").Copy
Range("rngBarcodeInput").PasteSpecial Paste:=xlPasteFormats
Range("rngShipCheckInputFieldsNoBarcode").ClearContents
Range("rngEditStatus").ClearContents
End If
Set KeyCells = Range("C7")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
Range("C15").Value = Range("B15").Value
End If
Application.EnableEvents = True
End Sub
我认为这应该奏效:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("rngBarcodeInput")) Is Nothing Then
If Application.CutCopyMode = xlCopy Then
Application.Undo
Target.PasteSpecial Paste:=xlPasteValues
End If
Range("DJ5").Copy
Range("rngBarcodeInput").PasteSpecial Paste:=xlPasteFormats
Range("rngShipCheckInputFieldsNoBarcode").ClearContents
Range("rngEditStatus").ClearContents
End If
' no need for extra variable, just check address directly
'Dim KeyCells As Range
'Set KeyCells = Range("C7")
If Target.Address = "$C$17" Then Range("C15").Value = Range("B15").Value
Application.EnableEvents = True
End Sub
只需将这两种方法的代码放在一起。虽然其他答案似乎是正确的,但可能会有一些实例希望将这两个例程分开,因为它增加了额外的灵活性和调试的方便性 您可以通过将现有的两个例程重命名为任何您想要的,然后生成第三个例程来处理更改事件并调用两个单独的sub来实现这一点 在本例中,我们将重命名为
sub1
和sub2
,但显然您可以更改为提供更好描述的内容
将处理更改事件的例程。您只需调用
Sub1
&Sub2
,并传递由事件Target
获得的相同参数
Private Sub Worksheet_Change(ByVal Target As Range)
sub1 Target
sub2 Target
End Sub
您的原始例程,重命名为:
Private Sub sub1(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("rngBarcodeInput")) Is Nothing Then
If Application.CutCopyMode = xlCopy Then
Application.Undo
Target.PasteSpecial Paste:=xlPasteValues
End If
Range("DJ5").Copy
Range("rngBarcodeInput").PasteSpecial Paste:=xlPasteFormats
Range("rngShipCheckInputFieldsNoBarcode").ClearContents
Range("rngEditStatus").ClearContents
End If
Application.EnableEvents = True
End Sub
这样做的一个主要好处是,如果您有多个要使用代码的工作表,您可以将这两个例程复制到一个标准模块中。然后每个工作表都会有调用这些例程的工作表\u Change()
事件。
如果您曾经不得不修改这两个sub中的任何一个,您只需要修改一次,而不必一页一页地进行更新。第二个代码将不起作用,因为它将引发它所处理的事件。
Private Sub sub2(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("C7")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
Range("C15").Value = Range("B15").Value
End Sub