简体   繁体   English

在Excel错误1004中使用VBA剪切和粘贴循环

[英]Cut and Paste Loop with VBA in Excel Error 1004

For my current project, I'm trying to move a group of cells down in a worksheet up to a certain number of rows. 对于我当前的项目,我试图在工作表中将一组单元格向下移动到一定数量的行。 i figured the easiest way to do this is to cut the 2nd to last group and paste it in place of the last group, then cut the 3rd to last group and paste it in the place where the 2nd to last was and so on (since I don't want to be the list to be ever expanding) using the following code: 我认为最简单的方法是切割第二组到最后一组并将其粘贴到最后一组,然后将第三组切割到最后一组并将其粘贴到第二组到最后一组的位置,依此类推(从那时起)我不想成为要扩展的列表)使用以下代码:

For i = 298 To 8 Step -5
Sheets("Calc").Range(Cells(i, 130), Cells(i + 4, 130)).Cut
Sheets("Calc").Range(Cells(i + 5, 1)).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Next i

However, I always get a runtime error 1004 on the second line of code. 但是,我总是在第二行代码上得到运行时错误1004。

Can anyone help me out? 谁能帮我吗?

Use this one instead: 改为使用这个:

With Sheets("Calc")
   For i = 298 To 8 Step -5
       .Range(.Cells(i, 130), .Cells(i + 4, 130)).Cut Destination:=.Cells(i + 5, 130)
   Next i
End With

Howerver, as I see, you can do it without loop: 如我所见,你可以在没有循环的情况下完成它:

With Sheets("Calc")
    .Range(.Cells(8, 130), .Cells(302, 130)).Cut Destination:=.Cells(13, 130)
End With

Btw, I've changed your destination range from Sheets("Calc").Cells(i + 5, 1) to Sheets("Calc").Cells(i + 5, 130) (as I understood your question, you want to move cells down, but not across) 顺便说一句,我已经将目的地范围从Sheets("Calc").Cells(i + 5, 1)改为Sheets("Calc").Cells(i + 5, 130) (据我所知你的问题,你想要的)移动细胞,但不能跨越细胞)

You cannot use PasteSpecial with Cut 您不能将PasteSpecialCut

You can either copy and paste as values 您可以复制并粘贴为值

OR 要么

You can cut and paste 你可以剪切和粘贴

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

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