ReDim Preserve eines 2D Arrays
18.10.2016 08:54:48
baschti007
Ich habe einen Bereich in A1:B10 in A1 befinden sich leere Zellen , dieses Spalte soll somit nicht in das Array so weit so gut .
Nur definiere ich das Array mit ReDim Preserve auf die Anzahl die in das Array gekommen sind nur leider funktioniert das mit dem 2D Array nicht , wenn ich nur 1D Array habe läuft es .
Fehler : Index Außerhalb des gültigen Bereiches
Gruß Basti
Option Explicit
Sub DoIT()
Dim x As Long, c As Long
Dim lngCount As Long
Dim myArray As Variant
Dim tmp()
myArray = Range("A1:B10")
ReDim tmp(1 To UBound(myArray, 1), 1 To UBound(myArray, 2))
For x = LBound(myArray, 1) To UBound(myArray, 1)
If myArray(x, 1) "" Then
lngCount = lngCount + 1
For c = LBound(myArray, 2) To UBound(myArray, 2)
tmp(lngCount, c) = myArray(x, c)
Next
End If
Next
ReDim Preserve tmp(1 To lngCount, 1 To UBound(myArray, 2))
For x = 1 To UBound(tmp, 1)
For c = 1 To UBound(tmp, 2)
Cells(x, c + 4) = tmp(x, c)
Next
Next
End Sub