简体   繁体   English

循环浏览EXCEL VBA词典

[英]Looping Through EXCEL VBA Dictionary

I have a VBA Dictionary with the following data: 我有一个VBA词典,其中包含以下数据:

ID       NAME       POSITION 
5008004  John Doe   00120096
5008002  John Doe2  00117886
5010010  John Doe3  00117886

I have an Excel documented with the following cells: 我有一个包含以下单元格的Excel文档:

POSITION    SUPERVISOR_NAME
00117886    John Doe
00117886    John Doe2
00117886    John Doe3

Current Excel VBA code loops through the dictionary in the following fashion: 当前的Excel VBA代码以以下方式在字典中循环:

If SUPERVISOR_NAME <> "" Then
    For Each myKey In superDictionary.Keys
            If superDictionary(myKey) = SUPERVISOR_NAME Then
                SUPERVISOR_NAME = myKey
                Exit For
            End If
        Next
End If

Resulting in replacing the JOHN DOE names with their associated IDs no matter what. 无论如何,都会用其关联的ID替换JOHN DOE名称。

Question: How do I go about replacing the JOHN DOE names with their associated IDs BUT only when the POSITION and SUPERVISOR_NAME from EXCEL matches the Dictionary or ELSE submit nothing. 问题:仅当EXCEL中的POSITION和SUPERVISOR_NAME与Dictionary匹配或ELSE不提交任何内容时,如何使用相关的ID BUT替换JOHN DOE名称。

It doesn't seem like you are properly utilizing one of the most powerful features of a Scripting.Dictionary object; 似乎您没有适当地利用Scripting.Dictionary对象的最强大功能之一; that being its fast retrieval capabilities. 那是它的快速检索功能。 You essentially want to perform a lookup with two-column criteria so use the two columns as your key and the ID as the Item . 本质上,您希望使用两列条件执行查找,因此请使用两列作为 ,将ID用作Item

dictionary_lookup

Option Explicit

Sub supervisorIDs()
    Dim d As Variant, dict As Object
    Dim v As Long, vVALs As Variant

    Set dict = CreateObject("Scripting.Dictionary")
    dict.comparemode = vbTextCompare  'default is vbbinarycompare

    With Worksheets("Sheet4")
        'get values from worksheet
        vVALs = .Range(.Cells(2, 1), .Cells(Rows.Count, 3).End(xlUp)).Value2
        'build dictionary
        For v = LBound(vVALs, 1) To UBound(vVALs, 1)
            'overwrite method - faster (no error control)
            'writes name&position as key, ID as item
            dict.Item(Join(Array(vVALs(v, 2), vVALs(v, 3)), ChrW(8203))) = vVALs(v, 1)
        Next v

        'loop through the second table
        For v = 2 To .Cells(Rows.Count, 6).End(xlUp).Row
            d = Join(Array(.Cells(v, 6).Value2, .Cells(v, 5).Value2), ChrW(8203))
            If dict.exists(d) Then _
                .Cells(v, 7) = dict.Item(d)
        Next v
    End With
End Sub

dictionary_lookup_results

Do you mean like this? 你是这个意思吗

Assuming your supervisor names are in column B starting in row 2: 假设您的主管姓名在B栏中,从第2行开始:

Dim r As Long
Dim supervisorName As Range

For Each supervisorName In Range("B2:B" & Cells.(Rows.Count, 2).End(xlUp).Row).Cells
    If superDictionary.Exists(supervisorName.Value) Then
        r = 2 '// First row with data in
        For Each key In superDictionary.Keys
            If superDictionary(key) = supervisorName.Value And supervisorName.Row = r Then
                supervisorName.Value = key
                Exit For
            Else
                r = r + 1
            End If
        Next
    End If
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM