简体   繁体   English

宏根据特定单元格的值向左重复复制粘贴

[英]Macro to repeat copy paste to the left based on the value of a particular cell

I am completely new to VBA.我对 VBA 完全陌生。 I have a spreadsheet that needs to align data to dates.我有一个需要将数据与日期对齐的电子表格。 The dates change dynamically as the sheet is updated.日期随着工作表的更新而动态变化。

Basically the macro below moves the data over one column to the left (replaces column J with data from column K to Q) and clears the existing data from Q. the data is a combination of just values, formulas and formatting.基本上,下面的宏将数据移到左侧的一列上(用从 K 列到 Q 的数据替换 J 列)并清除 Q 中的现有数据。数据只是值、公式和格式的组合。 The below macro works however I need it to repeat itself the number of times whatever the value is in cell E3 (this cell will take into account time lag to realign the data).下面的宏可以工作,但是我需要它重复自己的次数,无论单元格 E3 中的值是什么(该单元格将考虑时间延迟以重新对齐数据)。

So basically can someone please help this repeat this macro based on the value in E3 if it is greater then 1. Also I get a bug clearing the cells after it is repeated once because the cells are already clear so maybe run the first part as is then adding an IF ("E3") > 1 then moving Range("K6:P500") the number of times in E3.所以基本上有人可以帮助这个根据 E3 中的值重复这个宏,如果它大于 1。此外,我在重复一次后得到一个清除单元格的错误,因为单元格已经清除,所以可能按原样运行第一部分然后添加一个IF ("E3") > 1然后在IF ("E3") > 1移动Range("K6:P500")的次数。 I have tried to do this but I don't know how to get the repeat and the IF I put together didn't really work.我试过这样做,但我不知道如何获得重复,而且我放在一起的 IF 并没有真正起作用。

Thanks again so much for any help of suggestions!再次感谢任何建议的帮助!

' 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

Assuming what you mean is to move the whole of columns the number contained in E3 rows to the right, this will do what you wanted.假设您的意思是将 E3 行中包含的数字的整个列向右移动,这将执行您想要的操作。

It's easier to work with moving ranges if you use Cells notation rather than A1:B1 .如果您使用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

This is if you actually need the PasteSpecial .这是如果您确实需要PasteSpecial If not then you can just use:如果没有,那么您可以使用:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM