[英]Excel VBA cut and paste rage repeatedly
I am a complete novice in using VBA and trying to cut and copy a range repeatedly.我是使用 VBA 并尝试反复剪切和复制范围的新手。 I was searching and trying my code around, but gets nowhere...
我正在搜索并尝试我的代码,但一无所获......
For example, say the whole data in a range (A1:M1084) consist of blocks of 124 rows and 13 columns.例如,假设一个范围 (A1:M1084) 中的整个数据由 124 行和 13 列的块组成。 So the first block is in (A1:M124).
所以第一个块在 (A1:M124) 中。 The second data block is currently in (A125:M248), and would like to cut it and paste it next to the first block, leaving a column space (N) in between, to (O1:AA248).
第二个数据块当前位于 (A125:M248) 中,并且想将其剪切并粘贴到第一个块旁边,在中间留出一个列空间 (N) 到 (O1:AA248)。 Then, repeat the process until the end of the row (this time 1084, but can be any other number), keep pasting the next block (A249: M372) next to the second block, to (AC1:AO248), and so forth...
然后,重复该过程直到行尾(这次是 1084,但可以是任何其他数字),继续将下一个块(A249:M372)粘贴到第二块旁边,到(AC1:AO248),依此类推...
If anyone could please help, much appreciated!如果有人可以请帮助,非常感谢!
Try something like this.尝试这样的事情。
Sub MoveBlocks()
Dim rowCount As Integer, colCount As Integer
Dim inRow As Integer
Dim blockSize As Integer
Dim colOffset As Integer
blockSize = 124
rowCount = Range("A1").CurrentRegion.Rows.Count
colCount = Range("A1").CurrentRegion.Columns.Count
For inRow = blockSize + 1 To rowCount Step blockSize
colOffset = Int(inRow / blockSize) * (colCount + 1)
Range(Cells(1, 1), Cells(blockSize, colCount)).Offset(0, colOffset).Value2 = Range(Cells(1, 1), Cells(blockSize, colCount)).Offset(inRow - 1, 0).Value2
Range(Cells(1, 1), Cells(blockSize, colCount)).Offset(inRow - 1, 0).ClearContents
Next inRow
End Sub
After all, I got an answer myself with so many trials and errors.毕竟,经过这么多试验和错误,我自己得到了答案。 Posting if it maybe of any use.
发布它是否可能有用。 Also, if there is any other more efficient code, would be happy to have!
另外,如果有任何其他更有效的代码,将很高兴拥有!
Sub Cutrange()
Dim i As Long
Dim Lrow As Long
Lrow = Cells(Rows.Count, 1).End(xlUp).Row
Dim oRange As Range, dRange As Range
Set oRange = Range(Cells(1, 1), Cells(124, 14))
Set dRange = Cells(1, 1)
For i = 1 To Lrow
oRange.Offset(124 * i).Cut Destination:=dRange.Offset(, 14 * i)
Next i
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.