我在 Excel 2003 中有一个 VBA 对象,当通过流数据获得某些值时,它会触发三个简单的宏。它运行良好。我想打开一个重复的工作表,但具有不同的流数据,并在各自的工作表上触发宏。它现在可以使用,但仅适用于我当前选择的工作表。每个工作表中都有对象和宏。
对象监控三个单元格,当第一个单元格被触发时,继续监控下一个单元格,当触发时监控下一个单元格,然后重复。
我会帮助他们同时在各自的电子表格上运行。
这是 VBA 对象:
Private Sub Worksheet_Calculate()
Static oldval1
Static oldval2
Static oldval3
Static LastAction As Integer
' Initial state will be 0, neither Fast nor Slow
Const Fast As Integer = 1
Const Fast2 As Integer = 2
Const Slow As Integer = 3
Application.EnableEvents = False
If Range("I1").Value = "1" And oldval1 <> "1" And LastAction <> Fast Then
PasteFast
LastAction = Fast
ElseIf Range("Q1").Value = "1" And oldval2 <> "1" And LastAction <> Slow Then
PasteFast2
LastAction = Fast2
ElseIf Range("Y1").Value = "1" And oldval3 <> "1" And LastAction <> Slow Then
PasteSlow
LastAction = Slow
End If
oldval1 = Range("I1").Value
oldval2 = Range("Q1").Value
oldval3 = Range("Y1").Value
Application.EnableEvents = True
End Sub
而且,这里有三个宏——它们本质上是相同的——它们从同一个位置复制,但粘贴到不同的位置。PasteSlow 和 PasteFast 在一个模块上,而 PasteFast2 在第二个模块上(无缘无故)。
Sub PasteSlow()
'
' PasteSlow Macro
'
'
Application.ScreenUpdating = False
Range("G5:G57").Select
Selection.Copy
Range("H5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("K5:K57").Select
Application.CutCopyMode = False
Selection.Copy
Range("L5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
End Sub
Sub PasteFast()
'
' PasteFast Macro
'
'
Application.ScreenUpdating = False
Range("g5:g57").Select
Selection.Copy
Range("P5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("k5:k57").Select
Application.CutCopyMode = False
Selection.Copy
Range("T5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
End Sub
Sub PasteFast2()
'
' PasteFast2 Macro
'
'
Application.ScreenUpdating = False
Range("g5:g57").Select
Selection.Copy
Range("x5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("k5:k57").Select
Application.CutCopyMode = False
Selection.Copy
Range("ab5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
End Sub
对于任何代码格式错误,我深表歉意。