Credo che la soluzione di Jook sia buona come quella che si otterrà se è importante ottenere gli intervalli di origine in un array. Tuttavia, penso che la soluzione dovrebbe includere istruzioni sull'estrazione di valori da un array frammentato. Questo non è difficile ma la sintassi è oscura.
Non riesco a ottenere neanche l'istruzione Union
. Presumo che ci sia qualcosa nel contesto che causa l'errore che non posso duplicare.
Il codice seguente mostra che i due intervalli sono uguali e che solo il primo sottogruppo viene caricato su un array come riportato. Termina con un approccio alternativo che potrebbe essere soddisfacente.
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
fonte
2012-11-06 15:38:05
basta usare split applicato all'indirizzo dell'indirizzo, separatore "," –