是否可以复制一定范围的数据,并且在粘贴时,它将自动分为多列(每列具有相同的行数)。如下图所示,我想复制数据B2:B9,然后将其自动粘贴到D2:D8,E2:E8,F2:F8。 figure of question
我是vba的新手,到目前为止,我尝试使用宏录制但无法完成。对不起我的英语,请帮忙!
Range("B2:B19").Select
Selection.copy
Range("D2:D8").Select
ActiveSheet.Paste
Range("E2:E8").Select
ActiveSheet.Paste
Range("F2:F8").Select
ActiveSheet.Paste
答案 0 :(得分:0)
Option Explicit
Sub TEST()
With ThisWorkbook.Worksheets("Sheet1")
.Range("B2:B8").Copy .Range("D2:D8")
.Range("B9:B15").Copy .Range("E2:E8")
.Range("B16:B19").Copy .Range("F2:F4")
End With
End Sub
该代码不会自动划分整个范围,因为您没有提供希望划分范围的任何数字。
答案 1 :(得分:0)
您只需要调整大小和偏移功能以及一些基本的数学技能即可。
Option Explicit
Sub splitCopy()
Dim srng As Range, trng As Range
Dim i As Long, n As Long, xsplit As Long, xrows As Long
xsplit = 3 'number of target columns
xrows = 7 'number of target rows
With Worksheets(1)
Set srng = .Range("B2:B19")
Set trng = .Range("D2")
For i = 0 To xsplit - 1
n = Application.Min(xrows, srng.Rows.Count - xrows * i)
srng.Resize(n, 1).Offset(i * xrows, 0).Copy _
Destination:=trng.Offset(0, i)
Next i
End With
End Sub