首页 > 解决方案 > 具有复合键字典的 VBA 数组代码

问题描述

我试图将大型 excel 数据循环到字典中并拉出。这是我正在尝试的代码我遇到了很多错误。请帮我写下我的代码~!!

在此处输入图像描述

Sub Me()

    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")
    
    Set ws1 = Sheets("Sheet1")
    lastrow = ws1.Range("B" & ws1.Rows.Count).End(xlUp).Row
    LastCol = ws1.Cells(Range("C2").Row, ws1.Columns.Count).End(xlToLeft).Column - 1
    vd = WorksheetFunction.Transpose(WorksheetFunction.Transpose(ws1.Range("C3").Resize(1, LastCol - 1)))
    va = ws1.Range("b4").Resize(lastrow - 2, 1)
    For i = LBound(va) To UBound(va)
        dict.Add addToString(vd(1), va(i, 1)), ws1.Cells(i + 3, 3).Value

        Debug.Print dict("uniqueXXXuniqueYYY")
    Next i
       
End Sub


Public Function addToString(ParamArray myVar() As Variant) As String

    Dim cnt     As Long
    Dim val     As Variant
    Dim delim   As String: delim = "unique"

    For cnt = LBound(myVar) To UBound(myVar)
        addToString = addToString & delim & myVar(cnt)
    Next cnt

End Function

标签: arraysvbadictionarykeycomposite

解决方案


推荐阅读