0

我写了一个简单的宏来复制和粘贴一行相邻的两列,然后我添加了 app.ontime 来自动运行复制和粘贴宏。我有多行数据要应用我的宏。有什么想法吗?

Public Sub PasteDynamicData()

'runs TestKDdata every Xmin

Sheets("MOVINGAVGDATAFromKD").Range("C4").Copy

Range("J4").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False

Sheets("MOVINGAVGDATAFromKD").Range("I4:j4").Insert _
shift:=xlDown

Sheets("MOVINGAVGDATAFromKD").Range("D4").Copy

Range("m4").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False

Sheets("MOVINGAVGDATAFromKD").Range("L4:M4").Insert _
shift:=xlDown

Sheets("MOVINGAVGDATAFromKD").Range("i87:m87").ClearContents
End Sub

 Public Sub UpdateDataClock()
 'Clock that prompts running of PasteDynamicData
 Sheets("MOVINGAVGDATAFromKD").Select
 Call PasteDynamicData
 Nexttick = Now + TimeValue("00:00:30")
 Application.OnTime Nexttick, "updatedataclock"

        If Time >= TimeValue("16:00:00") Then
        Application.OnTime Nexttick, "updatedataclock", , False
End If
End Sub


(I declared "nexttick as date" in the module).
4

1 回答 1

0

你能解释一下到底是什么问题吗?您想要修复或改进什么?有什么不工作吗?

无论如何,请尝试以下操作:

Public Sub PasteDynamicData()

Dim xlMovingSheet As Worksheet
Set xlMovingSheet = ActiveWorkbook.Worksheets("MOVINGAVGDATAFromKD")

xlMovingSheet.Range("C4").Copy

xlMovingSheet.Range("J4").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False

xlMovingSheet.Range("I4:j4").Insert _
shift:=xlDown

xlMovingSheet.Range("D4").Copy

xlMovingSheet.Range("m4").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False

xlMovingSheet.Range("L4:M4").Insert _
shift:=xlDown

xlMovingSheet.Range("i87:m87").ClearContents

Call UpdateDataClock
End Sub

对于更新部分,设置您想要的任何时间增量。现在它设置为每 30 秒运行一次 PasteDynamicData。

Public Sub UpdateDataClock()

Application.OnTime Now() + TimeValue("00:00:30"), "PasteDynamicData"

If Time >= TimeValue("16:00:00") Then
    Application.OnTime EarliestTime:=Now() + TimeValue("00:00:30"), Procedure:="PasteDynamicData", Schedule:=False
End If

End Sub
于 2013-06-06T01:08:42.147 回答