在 vba 中向下移动行范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13496955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:28:38  来源:igfitidea点击:

move range of rows down in vba

vbarowsmoveoffset

提问by Trung Tran

I am trying to select the first 7 rows of my spreadsheet (the exact number could vary) and move it 32 rows down (number of rows to move down could also vary). Could someone help me with the code? I have tried:

我正在尝试选择电子表格的前 7 行(确切数字可能会有所不同)并将其向下移动 32 行(向下移动的行数也可能会有所不同)。有人可以帮我写代码吗?我试过了:

Worksheets("Report").Cells(x1, 5).EntireRow.Offset(32, 0).Select

I also tried

我也试过

for i = 1 to 7
set x1 = worksheets("Report").Cells(i, 5)
Rows(x1).EntireRow.Offset(32, 0).Select

Won't work either. Thanks in advance for the help!

也行不通。在此先感谢您的帮助!

回答by Doug Glancy

This does what you ask, and if there are any rows after 32 shifts them down:

这会按照您的要求执行,如果 32 之后有任何行,则将它们向下移动:

Sub MoveRowsDown()
Dim NumRows As Long
Dim TargetRow As Long
Dim ws As Excel.Worksheet

NumRows = 7 'change as necessary
TargetRow = 33 'change as necessary
Set ws = ActiveSheet ' change as necessary

ws.Range("A1").Resize(NumRows).EntireRow.Cut
ws.Range("A" & TargetRow + NumRows).EntireRow.Insert shift:=xlDown
End Sub

EDIT: Here's a version that just cuts and pastes, with no fancy inserting:

编辑:这是一个只是剪切和粘贴的版本,没有花哨的插入:

Sub MoveRowsDown()
Dim NumRows As Long
Dim TargetRow As Long
Dim ws As Excel.Worksheet

NumRows = 7 'change as necessary
TargetRow = 33 'change as necessary
Set ws = ActiveSheet ' change as necessary

ws.Range("A1").Resize(NumRows).EntireRow.Cut Destination:=ws.Range("A" & TargetRow)
End Sub

回答by Sico

try this

尝试这个

Sub marine()

ActiveSheet.Rows("32:38").Value = ActiveSheet.Rows("1:7").Value
ActiveSheet.Rows("1:7").Clear

End Sub

replace activesheet with your sheet name. activesheets are not the best

用您的工作表名称替换 activesheet。activesheets 不是最好的

回答by Gerard ONeill

Just found this:

刚刚发现这个:

range("A1:C6").Cut range("A10")

Sweet!

甜的!

I'd also try:

我也会尝试:

rows("1:7").cut rows("32")