Nối hai mảng trong vba excel

Bạn cần phải nối hai mảng riêng biệt lại với nhau để có thể xử lý được yêu cầu mà mình đang cần kíp. Ví dụ như lọc danh sách duy nhất từ hai mảng chẳng hạn.


Đoạn code dưới đây có thể giúp bạn nối hai mảng lại thành một mảng như bạn mong muốn.

Sub noimang()

    Dim i As Long, m As Long, n As Long
    Dim arr1 As Variant
    Dim arr2 As Variant
    Dim temp As Collection
    Dim xuat() As Variant
    arr1 = Sheet69.Range("H19:H21").Value
    arr2 = Sheet69.Range("I19:I22").Value
     m = UBound(arr1)
     n = UBound(arr2)
     
    Set temp = New Collection
    
    For i = 1 To m + n
        If i <= m Then
            temp.Add arr1(i, 1)
        Else
            temp.Add arr2(i - m, 1)
        End If
    Next

    ReDim xuat(1 To temp.Count, 1 To 1)
     
    For i = 1 To temp.Count
        xuat(i, 1) = temp.Item(i)
    Next i
    
    Sheet69.Range("J19").Resize(UBound(xuat, 1), UBound(xuat, 2)).Value = xuat
End Sub
Đăng nhận xét (0)
Mới hơn Cũ hơn