在搜索框中找不到项目,即使它们存在

时间:2017-07-27 19:34:11

标签: vba excel-vba excel

所以基本上我在Sheet 2中设置了一个搜索框,我使用条形码扫描器来扫描物品ID,当我点击搜索时,它会填充有关Sheet 1中找到的项目的信息。我有两种类型的项目系统中的ID,一个由字母和数字组成,另一个是严格的数字。我目前遇到的问题是,当我将严格的数字ID扫描到我的搜索框中时,它无法找到它,但如果我只是将表格1中的ID复制并粘贴到搜索框中,它就能找到它。我觉得好像它与我的搜索格式有关,也许它只是寻找文本,当我复制并粘贴到搜索框时,它们被格式化为文本?我不太确定,但任何见解都会非常感激。以下是我的代码。

Sub SearchBox()
Dim lastrow As Long
Dim i As Long, x As Long
Dim count As Integer

lastrow = Sheets("Charlotte Gages").Cells(rows.count, 1).End(xlUp).Row
i = Sheets("Gages").Cells(rows.count, 1).End(xlUp).Row + 1
For x = 2 To lastrow

If Sheets("Charlotte Gages").Cells(x, 1) = Sheets("Gages").Range("A1") Then
    Sheets("Gages").Cells(i, 1).Resize(, 7).Value = Sheets("Charlotte Gages").Cells(x, 1).Resize(, 7).Value
    i = i + 1
    count = count + 1
End If

Next x


If count = 0 Then
    MsgBox ("Cannot Find Gage, Please check Gage ID")
End If

End Sub

2 个答案:

答案 0 :(得分:0)

某处文本/值不相等。我在您的代码中插入了一个msgbox,以便您可以准确地看到正在比较的内容。

在消息框标题中显示为真/假的更新

    Sub SearchBox()
Dim lastrow As Long
Dim i As Long, x As Long
Dim count As Integer

lastrow = Sheets("Charlotte Gages").Cells(Rows.count, 1).End(xlUp).Row
i = Sheets("Gages").Cells(Rows.count, 1).End(xlUp).Row + 1
For x = 2 To lastrow

'try with this to test
Dim anSwer As Integer

Dim TrueOrFalse As String
TrueOrFalse = Sheets("Charlotte Gages").Cells(x, 1) = Sheets("Gages").Range("A1")

anSwer = MsgBox("your value in Charlotte Gauges Cell " & Sheets("Charlotte Gages").Cells(x, 1).Address & " is """ & Sheets("Charlotte Gages").Cells(x, 1).Value & _
 """ while your other value in cell " & Sheets("Gages").Range("A1").Address & " is """ & Sheets("Gages").Range("A1").Value & """. Continue?", vbYesNo, Title:=TrueOrFalse)
If anSwer = vbNo Then
Exit Sub
End If
'End of test code, this can be deleted once you figure out your issue

If Sheets("Charlotte Gages").Cells(x, 1) = Sheets("Gages").Range("A1") Then
    Sheets("Gages").Cells(i, 1).Resize(, 7).Value = Sheets("Charlotte Gages").Cells(x, 1).Resize(, 7).Value
    i = i + 1
    count = count + 1
End If

Next x


If count = 0 Then
    MsgBox ("Cannot Find Gage, Please check Gage ID")
End If

End Sub

答案 1 :(得分:0)

所以我弄清楚出了什么问题。仅限数字的ID在工作表1中以文本形式存储,因此不允许我的搜索框找到它。我所要做的就是将它们全部转换成数字并修复一切。