Workbook.Match没有find它应该的值

我有以下function,添加一行到表。 它会在添加之前检查零件号码是否存在。不幸的是,匹配不会find现有数据,并悄悄地添加相同零件号码的多个副本。

任何人都可以指出我错过了什么?

Private Sub OkButton_Click() Dim LastRow As Long LastRow = LastRowOnSheet("Parts List") Dim sht As Worksheet Set sht = Worksheets("Parts List") 'Validate that the controls hold valid data If Not (Me.PartNumberTextBox.Value Like "######") Then MsgBox "Please enter a valid 6 digit Stackpole part number.", vbExclamation, "Invalid Part Number" Me.PartNumberTextBox.SetFocus Exit Sub End If If Me.DescriptionTextBox.Value = "" Then MsgBox "Please enter a description for this part.", vbExclamation, "Description Required" Me.DescriptionTextBox.SetFocus Exit Sub End If 'Validate that the part number does not already exist On Error Resume Next x = WorksheetFunction.Match(PartNumberTextBox.Value, sht.Range(sht.Cells(2, 3), sht.Cells(2, LastRow)), 0) If Not (x = "") Then x = x + 1 MsgBox ("Duplicate part number found at row: " & x) Exit Sub End If 'Add new row to the Parts List Sheet With Worksheets("Parts List").Range("A1") .Offset(LastRow, 0).Value = .Offset(LastRow - 1, 0).Value + 1 .Offset(LastRow, 1).Value = Me.DescriptionTextBox.Value .Offset(LastRow, 2).Value = Me.PartNumberTextBox.Value .Offset(LastRow, 3).Value = Me.StoresLocTextBox End With End Sub 

我认为PartNumberTextBox.Value是一个string,但单元格的值是整数。 将其转换为Integer可以解决问题

 x = WorksheetFunction.Match(Int(PartNumberTextBox.Value), sht.Range(sht.Cells(2, 3), sht.Cells(2, LastRow)), 0) 

它search错误的范围(行和列被颠倒)

 x = WorksheetFunction.Match(Int(PartNumberTextBox.Value), sht.Range(sht.Cells(2, 3), sht.Cells(LastRow, 3)), 0)