Split multidimensional array and then slice it [closed]

我与影子孤独终老i 提交于 2019-12-11 01:45:21

问题


I have a column of paths:

C:\Series1\Season1\Ep1
C:\Series1\Season2\Ep1
C:\Series2\Season1\Ep1
C:\Series2\Season2\Ep1
C:\Series3\Season1\Ep1

I now want to split the array into a multidimensional one, so it looks like this in the end:

+---+----+---------+---------+-----+
|   | 1  |    2    |    3    |  4  |
+---+----+---------+---------+-----+
| 1 | C: | Series1 | Season1 | Ep1 |
| 2 | C: | Series1 | Season2 | Ep1 |
| 3 | C: | Series2 | Season1 | Ep1 |
| 4 | C: | Series2 | Season2 | Ep1 |
| 5 | C: | Series3 | Season1 | Ep1 |
+---+----+---------+---------+-----+

I then have a function called unique(checkArray) that checks the number of unique values in an array. I want to have this function check every column 1 by 1.

Debug.Print uniqueValues(Column1)
Debug.Print uniqueValues(Column2)
Debug.Print uniqueValues(Column3)
Debug.Print uniqueValues(Column4)

How do I get the array into that formation and then checked?


回答1:


Sub SplitMe()
    Dim values As Variant
    values = ActiveSheet.Range("A1:A5")

    If Not IsArray(values) Then _
        Exit Sub

    Dim r As Integer
    Dim parts As Variant
    Dim partsMaxLenght As Integer
    Dim splitted As Variant
    ReDim splitted(LBound(values) To UBound(values))

    For r = LBound(values) To UBound(values)
        parts = VBA.Split(values(r, 1), "\")
        ' Split always returns zero based array so parts is zero based array
        If UBound(parts) + 1 > partsMaxLenght Then _
            partsMaxLenght = UBound(parts) + 1
        splitted(r) = parts
    Next r

    Dim matrix As Variant
    Dim c As Integer
    ReDim matrix(LBound(splitted) To partsMaxLenght, LBound(splitted) To UBound(splitted))

    For r = LBound(splitted) To UBound(splitted)
        parts = splitted(r)
        For c = 0 To UBound(parts)
            matrix(c + 1, r) = parts(c)
        Next c
    Next r

    uniqueValues matrix

End Sub

Private Sub uniqueValues(matrix As Variant)
    Dim r, c
    For r = LBound(matrix, 1) To UBound(matrix, 1)
        For c = LBound(matrix, 2) To UBound(matrix, 2)
            Debug.Print matrix(r, c)
        Next c
    Next r
End Sub

Output:
C:
D:
E:
F:
H:
etc.




回答2:


You would reference the array the same way you'd reference fields in a table. Like tables, arrays are also zero-based (meaning, the first column is referenced as 0 instead of 1). So, if your array is called MyArray, you would be doing something like:

Debug.Print MyArray(0) 
Debug.Print MyArray(1) 
Debug.Print MyArray(2)
Debug.Print MyArray(3)

Don't forget that you first have to Dim your array. If you know how many columns it has, you can explicitly Dim it like so:

Dim MyArray(0 to 3) As String

If not, you can leave it open-ended like so:

Dim MyArray() As String

You would have to loop through your array to get data from a specific line number, I don't think you can reference it any other way.




回答3:


Use the Split function to split your entries, like this

Dim episodes() As Variant
ReDim episodes(numEntries - 1) As Variant

For i = 0 to numEntries - 1
    episodes(i) = Split(paths(i), "/")
Next i

Debug.Print episodes(1)(1) ' = Series1
Debug.Print episodes(1)(2) ' = Season2

After that, ensuring duplicates should be as easy as looping over each item in the array.


Alternately, if you're just looking to find duplicates for the entire path, just create a collection object and load the item into the collection using the path as the key. If the item has already been loaded the collection will throw an error.

On Error Goto Catch

Dim episodes as Collection
Set episodes = New Collection

For i = 0 to numEntries - 1
    episodes.add paths(i), paths(i)
Next i

Exit Sub
Catch:
    'duplicate found


来源:https://stackoverflow.com/questions/27427248/split-multidimensional-array-and-then-slice-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!