following a question I have asked recently I have created an array to copy data from one worksheet to another. My problem is that the array includes many columns I do not need in the new worksheet.
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
This code copies all the data from the array but I only want specific columns from the array. How would I have to go about this to get only columns A:E, AA, L, M:K?
Any help is appreciated. Thanks in advance.
Well if you are going to use arrays, then maybe something along these lines might help:
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
Or where you go about pasting the array values, you could change this to only paste specific columns from your original range... such as:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.