VBA Copy&粘贴多行的相邻列

时间:2013-06-05 22:06:00

标签: excel vba row

我写了一个简单的宏来复制并粘贴两个相邻列的一行,然后我添加了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).

1 个答案:

答案 0 :(得分: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