简体   繁体   中英

VBA Array function to copy columns

Just wanted to seek some help as I'm not very familiar with using array function.

Basically, I have my source data which has a range of data from columns A to H.

This formula copies column A to my destination. But I also need data from source columns D to H copied to my destination starting at column C.

How can I modify this to what I need?

How can I filter and delete blank rows on the Source tab?

Dim Source As Worksheet, Destination As Worksheet
Set Source = ThisWorkbook.Worksheets("CAN Daily Hours Summary")
Set Destination = ThisWorkbook.Worksheets("Adjustment_Data")
    
Dim arr As Variant
arr = Source.Range("A1").CurrentRegion

Dim i As Long
For i = LBound(arr, 1) + 1 To UBound(arr, 1)
    arr(i, 1) = arr(i, 1)
Next i

Destination.Range("A1").CurrentRegion.Offset(1).ClearContents

Dim rowcount As Long, columncount As Long
rowcount = UBound(arr, 1)

Destination.Range("A2").Resize(rowcount).Value = arr

Thank you for any help.

Transfer Data (One Array Only)

The Code

Option Explicit

Sub transferData()
    
    Const FirstCell As String = "A1"
    Const destOffs As Long = 1
    Const FirstCol As Long = 2
    Const Offs As Long = 2
    
    With ThisWorkbook
        Dim Source As Worksheet
        Set Source = .Worksheets("CAN Daily Hours Summary")
        Dim Destination As Worksheet
        Set Destination = .Worksheets("Adjustment_Data")
    End With
    
    Dim arr As Variant
    arr = Source.Range(FirstCell).CurrentRegion
    
    Dim RowsCount As Long
    RowsCount = UBound(arr, 1)
    Dim ColsCount As Long
    ColsCount = UBound(arr, 2)
    Dim NumOfCols As Long
    NumOfCols = ColsCount - Offs
    
    Dim i As Long
    Dim j As Long
    For i = 1 To RowsCount
        For j = FirstCol To NumOfCols
            arr(i, j) = arr(i, j + Offs)
        Next j
    Next i
    
    With Destination.Range(FirstCell).CurrentRegion.Offset(destOffs)
        .ClearContents
        .Resize(RowsCount, NumOfCols).Value = arr
    End With
    
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM