首页 > 解决方案 > 将两个工作表中的数据与 vba 中的主键匹配(发生错误 91)

问题描述

我尝试匹配两个工作表中的数据。我在每个工作表中都有一个主键和一列分配的数据。目标是匹配分配的数据而不在第三个工作表中显示主键。我也愿意接受其他解决方案的想法。非常感谢!

在调试模式下,该行:

lgfindRow = ThisWorkbook.Sheets("Labeltexte").Columns(1).Find(strPIN, lookat:=xlWhole, LookIn:=xlValues, MatchCase:=True).row

突出显示。

完整的子:

Sub MatchData()

    Dim datasheet As Worksheet
    Dim lgCount As Long
    Dim intlastRow As Integer
    Dim strPIN As String
    Dim intcolumn As Integer
    Dim intcheck As Integer
    Dim intrange As Integer
    Dim lgfindRow As Long
    Dim intcancel As Integer
    
    Set datasheet = ThisWorkbook.Worksheets("Produktdaten")
    intlastRow = ThisWorkbook.Sheets("Artikelnummern").UsedRange.SpecialCells(xlCellTypeLastCell).row
    
    For lgCount = 1 To intlastRow
        intcolumn = 3
        intcheck = 1
        intcancel = 0
        strPIN = ThisWorkbook.Sheets("Artikelnummern").Cells(lgCount, 1).Value
        datasheet.Cells(lgCount, 1).Value = strPIN
        datasheet.Cells(lgCount, 2).Value = ThisWorkbook.Sheets("Artikelnummern").Cells(lgCount, 2).Value
        lgfindRow = ThisWorkbook.Sheets("Labeltexte").Columns(1).Find(strPIN, lookat:=xlWhole, LookIn:=xlValues, MatchCase:=True).row**
        Do Until ThisWorkbook.Sheets("Labeltexte").Cells(lgfindRow + intcheck, 1).Value <> "" Or intcancel = 10
            intcheck = intcheck + 1
            intcancel = intcancel + 1
        Loop
        For intrange = 0 To intcheck - 1
            datasheet.Cells(lgCount, intcolumn).Value = ThisWorkbook.Sheets("Labeltexte").Cells(lgfindRow + intrange, 2).Value
            intcolumn = intcolumn + 1
        Next
    Next
    
End Sub

标签: excelvba

解决方案


推荐阅读