简体   繁体   English

根据单元格值复制和粘贴行

[英]Copy and Paste Rows based on cell value

I am trying to copy rows 10:12 and paste it underneath for every time the value in D8 is over 1 ( ie D8 = 2 then paste once, if the value equals 3 then paste twice, and so on). 我试图复制行10:12,并在每次D8中的值超过1时将其粘贴在下面(即D8 = 2,然后粘贴一次,如果值等于3,则粘贴两次,依此类推)。 I don't even know where to start with this code so.... 我什至不知道从哪里开始用这段代码。

You could do something like this I guess: 我猜你可以做这样的事情:

Sub LoopeyPasteyFantastico()
    Dim ValueOverOne As Long
    Dim i As Long
    Dim LastRow As Long

    ValueOverOne = Sheet1.Cells(8, 4).Value - 1

    For i = 1 To ValueOverOne
        Sheet1.Range("10:12").Select
        Selection.Copy
        LastRow = Sheet1.Range("A65536").End(xlUp).Row + 1
        Sheet1.Range(LastRow & ":" & LastRow).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    Next i
End Sub

Or a better alternative would be this(you don't have to use copy and paste): 或更好的替代方法是这样(您不必使用复制和粘贴):

Sub LoopeyPasteyFantasticoAlternative()
    Dim ValueOverOne As Long
    Dim i As Long
    Dim LastRow As Long

    ValueOverOne = Sheet1.Cells(8, 4).Value - 1

    For i = 1 To ValueOverOne
        LastRow = Sheet1.Range("A65536").End(xlUp).Row + 1
        Sheet1.Range(LastRow & ":" & LastRow + 2).Value = Sheet1.Range("10:12").Value
    Next i
End Sub

This formula 这个公式

=IF(A$8>=1,A10,"") 

Will allow you to copy down based on the value in row eight, if you want to stick with just the value in D8 change the formula to 如果您只想保留D8中的值,则可以根据第八行中的值向下复制,将公式更改为

=IF($D$8>=1,A10,"") 

The formula will need to be updated every third row, so the forumalu in cell A16 would be 该公式将需要每三行更新一次,因此单元格A16中的公式

=IF(A$8>=2,A13,"")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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