Can you do something to the effect of “For each x

2019-09-06 07:50发布

The purpose of this code is to be able to track multiple properties of undefined "IDs," like name, total time, and occurrences while going through ton of data. As of right now I am using a jagged array to achieve this.

idArray = array of all gathered ID's in data

idProp = array of properties of each ID

I had everything working perfectly till I added the nested array.

I need to be able to check while going through the data if the ID is a new or not. with the name of each ID being held in the nested array i can't seem to be able to do this. Here is what i have so far:

    For Each ID In idArray()(1) '<-- does not target nested array correctly
            If ID = wbData.Sheets(1).Range("C" & Count) Then
                idPrs = True
            End If
    Next ID
    'adding ID if not present
    If idPrs = False Then
            idCount = idCount + 1
            MsgBox "new ID: " & wbData.Sheets(1).Range("C" & Count) & " on row " & Count
            ReDim Preserve idArray(0 To idCount)
            ReDim idProp(0 To 2)
            'adding starting values
            idProp(0) = wbData.Sheets(1).Range("C" & Count)
            idProp(1) = wbData.Sheets(1).Range("h" & Count)
            idProp(2) = 1
            idList(gidCount) = idProp
            'double array second "()" referrs to second array
            'MsgBox idArray(1)(0)

     End If

any ideas are welcome, even if they are done a completely different way.

标签: excel vba
2条回答
ら.Afraid
2楼-- · 2019-09-06 07:52

It's not entirely clear how you are declaring or popuplating your idArray.

This small demo of jagged arrays might give you some hints:

Sub Demo()
    Dim i As Long, j As Long
    Dim arr1 As Variant, arr2 As Variant
    Dim v As Variant

    ' Declare and populate a jagged array
    ReDim arr1(0 To 9)
    For i = LBound(arr1) To UBound(arr1)
        ReDim arr2(0 To i)
        For j = LBound(arr2) To UBound(arr2)
            arr2(j) = j
        Next
        arr1(i) = arr2
    Next

    ' Option 1 to iterate the array
    For i = LBound(arr1) To UBound(arr1)
        For j = LBound(arr1(i)) To UBound(arr1(i))
            Debug.Print arr1(i)(j)
        Next
        arr2 = arr1(i)
        For j = LBound(arr2) To UBound(arr2)
            Debug.Print arr2(j)
        Next
    Next

    ' Option 2 to iterate the array
    For Each arr2 In arr1
        For Each v In arr2
            Debug.Print v
        Next
    Next
End Sub
查看更多
Animai°情兽
3楼-- · 2019-09-06 08:08

Dictionaries could better tend to your problem. Here is a solution with a dictionary of dictionaries:

Dim probsById As New Scripting.Dictionary

Sub processId(id As String)

    If Not probsById.Exists(id) Then
        Dim props As New Scripting.Dictionary

        Call props.Add("id", id)
        Call props.Add("name", "Miller")
        Call props.Add("status", 1)

        Call probsById.Add(id, props)
    End If

End Sub

Sub test()
    Call processId("bar")
    Debug.Print probsById("bar").Item("name")
End Sub
查看更多
登录 后发表回答