如何正确引用字典项单元格地址

时间:2018-03-21 04:05:55

标签: excel vba excel-vba dictionary

如果WarehouseInventory上存在该值,如何将值从Scan Report工作表复制到dictionary items工作表?

我能够比较两个表(列)上的值,但无法引用字典项偏移单元格地址。

示例

If List.Exists(Inv_Data(i, 1)) Then
    .Cells(i, 1).Offset(0, 7).Value2 = "LPN SCANNED"
     SCAN_REPORT.Cells(x, 1).Offset(0, 1).Value2 = Inv_Data(i, 2)
Else
    .Cells(i, 1).Offset(0, 7).Value = "LPN NOT SCAN"
End If

SCAN_REPORT.Cells(x, 1)正在添加最后一行的值

Option Explicit
Private Sub Example()
    Dim SCAN_REPORT As Worksheet
    Set SCAN_REPORT = ActiveWorkbook.Worksheets("Scan Report")

    Dim List As New Scripting.Dictionary

    With SCAN_REPORT
        Dim Scn_LRow As Long
            Scn_LRow = .Cells(.Rows.Count, 1).End(xlUp).Row

        Dim Scn_Data() As Variant
            Scn_Data = .Range(.Cells(1, 1), .Cells(Scn_LRow, 1)).Value2

        Dim x As Long
        For x = LBound(Scn_Data) To UBound(Scn_Data) Step 1
            DoEvents
            Debug.Print Scn_Data(x, 1)
            On Error Resume Next 'resume if dupe
            List.Add Scn_Data(x, 1), x
            On Error GoTo 0
            Debug.Print Scn_Data(x, 1), x
        Next

        Dim INVENTORY_REPORT As Worksheet
        Set INVENTORY_REPORT = ActiveWorkbook.Worksheets("WarehouseInventory")

        With INVENTORY_REPORT
            Dim Inv_LRow As Long
                Inv_LRow = INVENTORY_REPORT.Cells(.Rows.Count, 1).End(xlUp).Row

            Dim Inv_Data() As Variant
                Inv_Data = .Range(.Cells(1, 1), .Cells(Inv_LRow, 7)).Value2

            Dim i As Long
            For i = LBound(Inv_Data) To UBound(Inv_Data) Step 1
                DoEvents

                If List.Exists(Inv_Data(i, 1)) Then
                     .Cells(i, 1).Offset(0, 7).Value2 = "LPN SCANNED"
                    SCAN_REPORT.Cells(x, 1).Offset(0, 1).Value2 = Inv_Data(i, 2)
'                    SCAN_REPORT.Cells(x, 1).Offset(0, 2).Value2 = Inv_Data(i, 3)
'                    SCAN_REPORT.Cells(x, 1).Offset(0, 3).Value2 = Inv_Data(i, 4)
                Else
                    .Cells(i, 1).Offset(0, 7).Value = "LPN NOT SCAN"
                End If

            Next
        End With
    End With

End Sub

2 个答案:

答案 0 :(得分:2)

在第二个循环中,x没有任何用处。您需要检索与关联的项目(在转储字典中的值时,表示为x的行号)。这样的事情可能有用:

SCAN_REPORT.Cells(List(Inv_Data(i, 1)), 1).Offset(0, 1).Value2 = Inv_Data(i, 2)

但是,如果您使用Jeeped在评论中建议的内容(项目覆盖中的快捷方式),这将仅标记或更新SCAN_REPORT中最后一次出现的数据。但是,在您当前的代码中,您只会更新第一次出现。

答案 1 :(得分:1)

我添加了一个.CompareMode选项并收紧了一些代码。 HTHS。

Option Explicit

Private Sub Example()
    Dim SCAN_REPORT As Worksheet
    Set SCAN_REPORT = ActiveWorkbook.Worksheets("Scan Report")

    Dim List As New Scripting.Dictionary
    List.comparemode = vbTextCompare      '<~~ ADDED!

    With SCAN_REPORT
        Dim Scn_LRow As Long
            Scn_LRow = .Cells(.Rows.Count, 1).End(xlUp).Row

        Dim Scn_Data() As Variant
            Scn_Data = .Range(.Cells(1, 1), .Cells(Scn_LRow, 1)).Value2

        Dim x As Long
        For x = LBound(Scn_Data) To UBound(Scn_Data) Step 1
            'Debug.Print Scn_Data(x, 1)
            List.Item(Scn_Data(x, 1)) = x
            'Debug.Print Scn_Data(x, 1), x
        Next

        Dim INVENTORY_REPORT As Worksheet
        Set INVENTORY_REPORT = ActiveWorkbook.Worksheets("WarehouseInventory")

        With INVENTORY_REPORT
            Dim Inv_LRow As Long
                Inv_LRow = .Cells(.Rows.Count, 1).End(xlUp).Row

            Dim Inv_Data() As Variant
                Inv_Data = .Range(.Cells(1, 1), .Cells(Inv_LRow, 7)).Value2

            Dim i As Long
            For i = LBound(Inv_Data, 1) To UBound(Inv_Data, 1)
                'DoEvents

                If List.Exists(Inv_Data(i, 1)) Then
                    .Cells(i, 1).Offset(0, 7).Value2 = "LPN SCANNED"
                    'i think this next line correction should resolve things
                    SCAN_REPORT.Cells(List.ITEM(Inv_Data(i, 1)), 1).Offset(0, 1).Value2 = Inv_Data(i, 2)
                    'SCAN_REPORT.Cells(List.ITEM(Inv_Data(i, 1)), 1).Offset(0, 2).Value2 = Inv_Data(i, 3)
                    'SCAN_REPORT.Cells(List.ITEM(Inv_Data(i, 1)), 1).Offset(0, 3).Value2 = Inv_Data(i, 4)
                Else
                    .Cells(i, 1).Offset(0, 7).Value = "LPN NOT SCAN"
                End If

            Next
        End With
    End With

End Sub