在我最近问过的一个问题后,我创建了一个数组,用于将数据从一个工作表复制到另一个工作表。我的问题是该数组包含我在新工作表中不需要的许多列。
x = Sheets("Sheet1").Range("A1048576").End(xlUp).Row
Set my_range = Sheets("Sheet1").Range("A2:AM" & x)
DirArray = my_range
Sheets("Sheet2").Select
Set Destination = Range("A3")
Destination.Resize(UBound(DirArray, 1), UBound(DirArray, 2)).Value = DirArray
此代码复制数组中的所有数据,但我只想要数组中的特定列。我怎么去这个才能得到A列:E,AA,L,M:K?
感谢任何帮助。提前谢谢。
答案 0 :(得分:0)
如果您打算使用数组,那么这些内容可能会有所帮助:
Sub foo()
Dim ColumnAarray() As Variant
Dim my_Arange As Range
LastRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
'Get one column's data
Set my_Arange = Sheets("Sheet1").Range("A2:A" & LastRow) ' get specific column
ReDim ColumnAarray(LastRow) 'Re size your array to fit data
ColumnAarray = my_Arange 'add data to array
''
''Repeat the process to copy more columns
''
Set Destination = Sheet2.Range("A3")
Destination.Resize(UBound(ColumnAarray, 1), UBound(ColumnAarray, 2)).Value = ColumnAarray 'add column A to Range A3 on Sheet2
End Sub
或者您要粘贴数组值的位置,您可以将其更改为仅粘贴原始范围中的特定列...例如:
Sub foo()
Dim ColumnAarray() As Variant
Dim my_Arange As Range
LastRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
Set my_Arange = Sheets("Sheet1").Range("A2:AM" & LastRow) ' get specific range
ReDim ColumnAarray(LastRow) 'Re size your array to fit data
ColumnAarray = my_Arange 'add data to array
For i = LBound(ColumnAarray) To UBound(ColumnAarray)
Sheet2.Range("A" & i) = ColumnAarray(i, 1) 'paste first column from original range
Sheet2.Range("B" & i) = ColumnAarray(i, 2) 'paste second column from original range into column B in Sheet 2
Next i
End Sub