[英]Macro to repeat copy paste to the left based on the value of a particular cell
我對 VBA 完全陌生。 我有一個需要將數據與日期對齊的電子表格。 日期隨着工作表的更新而動態變化。
基本上,下面的宏將數據移到左側的一列上(用從 K 列到 Q 的數據替換 J 列)並清除 Q 中的現有數據。數據只是值、公式和格式的組合。 下面的宏可以工作,但是我需要它重復自己的次數,無論單元格 E3 中的值是什么(該單元格將考慮時間延遲以重新對齊數據)。
所以基本上有人可以幫助這個根據 E3 中的值重復這個宏,如果它大於 1。此外,我在重復一次后得到一個清除單元格的錯誤,因為單元格已經清除,所以可能按原樣運行第一部分然后添加一個IF ("E3") > 1
然后在IF ("E3") > 1
移動Range("K6:P500")
的次數。 我試過這樣做,但我不知道如何獲得重復,而且我放在一起的 IF 並沒有真正起作用。
再次感謝任何建議的幫助!
' Week_update Macro
'
' Keyboard Shortcut: Ctrl+Shift+W
'
Range("K6:Q500").Select
Selection.Copy
Range("J6").Select
ActiveSheet.PasteSpecial Format:=2, Link:=1, DisplayAsIcon:=False, _
IconFileName:=False
Range("Q6").Select
Range("Q6:Q500").Select
Selection.SpecialCells(xlCellTypeConstants, 1).Select
Selection.ClearContents
End Sub
假設您的意思是將 E3 行中包含的數字的整個列向右移動,這將執行您想要的操作。
如果您使用Cells
表示法而不是A1:B1
則使用移動范圍更容易。
Sub Week_update()
Dim i As Long
i = Range("E6")
If i > 0 Then
' Copy range
Range(Cells(6, 10 + i), Cells(500, 18)).Copy
' Select range the same size but i columns to the right
Range(Cells(6, 10), Cells(500, 18 - i)).Select
' Paste special
ActiveSheet.PasteSpecial Format:=2, Link:=1, _
DisplayAsIcon:=False, IconFileName:=False
' Clear i columns on the right
Range(Cells(6, 18 - i), Cells(500, 18)).ClearContents
End If
End Sub
這是如果您確實需要PasteSpecial
。 如果沒有,那么您可以使用:
Sub Week_update()
Dim i As Long
i = Range("E6")
If i > 0 Then
' Copy range i columns to the left
Range(Cells(6, 11), Cells(500, 17)).Copy( _
Range(Cells(6, 11 - i), Cells(500, 17 - i)))
' Clear i columns on the right
Range(Cells(6, 17 - i), Cells(500, 17)).ClearContents
End If
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.