[英]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.