我尝试按特定顺序复制各种范围,然后将它们从工作簿粘贴到不同的工作簿中。
目前
我设定了范围,例如
Set rg = ws1.Range("A2:A" & i).Offset(rowOffset:=1, columnOffset:=0)
Set rg1 = ws1.Range("Z2:Z" & i).Offset(rowOffset:=1, columnOffset:=0)
Set rg2 = ws1.Range("C2:C" & i).Offset(rowOffset:=1, columnOffset:=0)
Set TradesCopy = Union(rg, rg1, rg2)
所以通常应该发生的是它应该按顺序粘贴在那些范围内(rg,rg1,rg2),
然而这是粘贴rg,rg2,rg1
有谁能告诉我我能做些什么来解决这个问题?
答案 0 :(得分:2)
如果您按照所需的顺序从工作表中构建值的二维数组,则可以按任意顺序放置它们,它将比多个Copy&粘贴操作。
Sub wqewqteq()
Dim a As Long, b As Long, c As Long, lr As Long
Dim vCOLs As Variant, vVALs As Variant, vSRCs As Variant
vCOLs = Array(1, 26, 3) 'could also be Array("A", "Z", "C")
With Worksheets("Sheet8")
lr = Application.Max(.Cells(.Rows.Count, "A").End(xlUp).Row, _
.Cells(.Rows.Count, "C").End(xlUp).Row, _
.Cells(.Rows.Count, "Z").End(xlUp).Row)
vSRCs = .Range(.Cells(2, "A"), .Cells(lr, "Z")).Value2
ReDim vVALs(1 To lr - 1, 1 To UBound(vCOLs) + 1)
For a = 1 To lr - 1
For b = LBound(vCOLs) To UBound(vCOLs)
vVALs(a, b + 1) = vSRCs(a, vCOLs(b))
Next b
Next a
.Cells(2, "AB").Resize(UBound(vVALs, 1), UBound(vVALs, 2)) = vVALs
End With
End Sub
我找不到具体的目的地,所以我在同一工作表上使用了AB2。
请记住,您在这里处理的是1-D和2-D阵列。默认情况下,1-D数组的下限为零(lbound(vCOLs)== 0);默认情况下,二维数组的每个等级的下限为1(lbound(vVALs,1)== lbound(vVALs,2)== 1)。