Uważam, że rozwiązanie Jook jest tak dobre, jak to się stanie, jeśli ważne jest doprowadzenie zakresów źródeł do tablicy. Jednak myślę, że rozwiązanie powinno zawierać instrukcje wyodrębniania wartości z poszarpanej tablicy. Nie jest to trudne, ale składnia jest niejasna.
Nie mogę uzyskać również oświadczenia Union
. Zakładam, że jest coś w kontekście, który powoduje niepowodzenie, którego nie mogę powielić.
Poniższy kod pokazuje, że dwa zakresy są takie same i że tylko pierwszy podzakres jest ładowany do tablicy, zgodnie z raportem. Kończy się alternatywnym podejściem, które może być satysfakcjonujące.
Option Explicit
Sub Test()
Dim CellValue() As Variant
Dim rng As Range
With Worksheets("Sheet1")
Set rng = .Range("A2:D9,A11:D12,A14:D15")
Debug.Print rng.Address
Set rng = Union(.Range("A2:D9"), .Range("A11:D12"), .Range("A14:D15"))
Debug.Print rng.Address
' The above debug statements show the two ranges are the same.
Debug.Print "Row count " & rng.Rows.Count
Debug.Print "Col count " & rng.Columns.Count
' These debug statements show that only the first sub-range is included the
' range counts.
CellValue = rng.Value
Debug.Print "Rows " & LBound(CellValue, 1) & " to " & UBound(CellValue, 1)
Debug.Print "Cols " & LBound(CellValue, 2) & " to " & UBound(CellValue, 2)
' As you reported only the first range is copied to the array.
rng.Copy Destination:=Worksheets("Sheet2").Range("A1")
' This shows you can copy the selected sub-ranges. If you can copy the
' required data straight to the desired destination, this might be a
' solution.
End With
End Sub
wystarczy zastosować podział zastosowany do adresu zakresu, separator to "," –