2016-04-12 16 views
0

Ich habe 3 eindimensionale Arrays nämlich Topic(), SubTopic() and Description(). Die maximale Anzahl von Einträgen in jedem dieser Arrays ist durch eine Variable noDescription begrenzt. Ich muss ein 3d Array aus diesen Arrays machen und es anzeigen. Mein Code ist:Excel-VBA Erstellen eines 3D-Array von 3 1D-Arrays

ReDim info(1 To Topic(), 1 To SubTopic(), 1 To Description()) 

    p = 1 
    Do 

    info(p, 0, 0) = Topic(p) 
    info(p, 1, 0) = SubTopic(p) 
    info(p, 0, 2) = Description(p) 

    MsgBox "array value" & info(p, 0, 0) & "and" & info(p, 1, 0) & "and" & info(p, 0, 2) 

    p = p + 1 

Loop Until p > noDescription 

Es gibt einen Typenkonfliktfehler beim Dimensionieren des Arrays. Ich fühle mich irgendwo falsch. Irgendeine Hilfe ?

+1

ich bin nicht sicher genau, was Sie erreichen wollen, aber versuchen 'ReDim info (1 bis UBound (Topic, 1), 1 bis UBound (Unterthema, 1), 1 To UBound (Beschreibung, 1)) ' – OldUgly

+3

Sind Sie sicher, dass Sie nicht nach einem 2D-Array mit 3 Spalten suchen? – oortCloud

+0

@OldUgly Danke dafür. Aber wie lege ich die Werte der 1d Arrays in ein 3d Array? – Nikky

Antwort

0

Hier ist eine Funktion, die eine beliebige Anzahl von 1-dimensionalen Arrays zu kleben zusammen verwendet werden können:

Function CombineArrays(ParamArray Arrays() As Variant) As Variant 
    'Takes a list of 1-dimensional arrays 
    'And returns a single array 
    'The arrays are assumed to have the same base 
    'But not necessarily the same length 
    'The arrays form the columns of the result 

    Dim A As Variant 
    Dim i As Long, j As Long, lb As Long, ub As Long, m As Long 

    'first find LBound -- assumed to be common 

    lb = LBound(Arrays(0)) 

    'now find the maximum ubound: 

    For i = LBound(Arrays) To UBound(Arrays) 
     If UBound(Arrays(i)) > ub Then ub = UBound(Arrays(i)) 
    Next i 

    'now redim and return final array 

    m = lb + UBound(Arrays) 
    ReDim A(lb To ub, lb To m) 

    For j = lb To m 
     For i = lb To UBound(Arrays(j - lb)) 
      A(i, j) = Arrays(j - lb)(i) 
     Next i 
    Next j 
    CombineArrays = A 
End Function 

Geprüft wie:

Sub Test() 
    Dim Larry(1 To 4) 
    Dim Curly(1 To 2) 
    Dim Moe(1 To 3) 
    Dim A As Variant 

    Larry(1) = 1 
    Larry(2) = 2 
    Larry(3) = 3 
    Larry(4) = 4 
    Curly(1) = "A" 
    Curly(2) = "B" 
    Moe(1) = "X" 
    Moe(2) = "Y" 
    Moe(3) = "Z" 

    A = CombineArrays(Larry, Curly, Moe) 
    Range("A1:C4").Value = A 
End Sub 

Nach dem Unter läuft, A1: C4 sieht aus wie :

1 A X 
2 B Y 
3  Z 
4