简体   繁体   English

VBA 循环剪切/粘贴/跳过

[英]VBA Loop cut/paste/skip

Very new to VBA and macros. VBA 和宏非常新。 Looking to build a macro that will cut the items in G2:H2, paste them in I2:J2, skip to the next even numbered row, cut those items (G4:H4), paste those into I4:J4, and so-on until the end of the spreadsheet.希望构建一个宏,将剪切 G2:H2 中的项目,将它们粘贴到 I2:J2,跳到下一个偶数行,剪切这些项目 (G4:H4),将它们粘贴到 I4:J4 中,依此类推直到电子表格结束。 The spreadsheet has roughly 11200 rows to sort through.电子表格大约有 11200 行要排序。 Any help is GREATLY appreciated!任何帮助是极大的赞赏!

Sub TotalMove()
'
' TotalMove Macro

a = 2
g = Sheet17.Cells(Rows.Count, "A").End(xlUp).Row

For a = 2 To g Step 2

    Range("G2:H2").Select
    Selection.Cut
    Range("I2:J2").Select
    ActiveSheet.Paste
  
Next a

End Sub

You almost had it, was just a case of using your row number in the loop.你几乎拥有它,只是在循环中使用你的行号的一个例子。

I made the variable names obvious for demo, set values rather than copy-paste, and specified the sheet in ranges.我使变量名称在演示中显而易见,设置值而不是复制粘贴,并在范围内指定工作表。

Sub TotalMove()

With Sheet17
    StartRow = 2
    EndRow = .Cells(Rows.Count, "G").End(xlUp).Row
    
    For CurrentRow = StartRow To EndRow Step 2
        .Range("I" & CurrentRow & ":J" & CurrentRow) = .Range("G" & CurrentRow & ":H" & CurrentRow).Value
        .Range("G" & CurrentRow & ":H" & CurrentRow).Clear
    Next CurrentRow
End With

End Sub

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

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