Create dictionary of lists in vba

后端 未结 3 1566
野趣味
野趣味 2020-12-15 07:07

I have worked in Python earlier where it is really smooth to have a dictionary of lists (i.e. one key corresponds to a list of stuff). I am struggling to achieve the same in

3条回答
  •  青春惊慌失措
    2020-12-15 07:48

    I am not that familiar with C++ and Python (been a long time) so I can't really speak to the differences with VBA, but I can say that working with Arrays in VBA is not especially complicated.

    In my own humble opinion, the best way to work with dynamic arrays in VBA is to Dimension it to a large number, and shrink it when you are done adding elements to it. Indeed, Redim Preserve, where you redimension the array while saving the values, has a HUGE performance cost. You should NEVER use Redim Preserve inside a loop, the execution would be painfully slow

    Adapt the following piece of code, given as an example:

    Sub CreateArrays()
    
    Dim wS As Worksheet
    Set wS = ActiveSheet
    
    Dim Flanged_connections()
    ReDim Flanged_connections(WorksheetFunction.CountIf(wS.Columns(1), _
        "Flanged_connections"))
    
    For i = 1 To wS.Cells(1, 1).CurrentRegion.Rows.Count Step 1
    
        If UCase(wS.Cells(i, 1).Value) = "FLANGED_CONNECTIONS" Then   ' UCASE = Capitalize everything
    
            Flanged_connections(c1) = wS.Cells(i, 2).Value
    
        End If
    
    Next i
    
    End Sub
    

提交回复
热议问题