2017-07-27 48 views
0

因此,基本上,我在工作表2中使用条形码扫描器来扫描项目ID,并且当我点击搜索时,它会填充关于该项目的信息在表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 
+0

也许尝试使用trim()函数 – sourceCode

+0

@sourceCode我将在哪里插入我的代码? – Jmaragno

+0

所有修剪功能都会去除前导空白和尾随空白。我认为这可能是问题。把所需的值作为参数修剪功能,看看是否是这个问题 – sourceCode

回答

0

某处文本/值不相等。我在你的代码中插入了一个msgbox,这样你就可以看到正在比较的内容。

WITH TRUE/FALSE显示模式为消息框标题

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 
+0

消息框工作完美,我仍然无法看到在哪里我的问题是因为我正在寻找的是大约4000行,我不想单击是4000次 – Jmaragno

+0

将if语句包装在i = 4000时启动消息框。 – PGCodeRider

+0

想通了我的问题,谢谢你的帮助! – Jmaragno

0

所以我想通了,什么是错误的更新。数字唯一的ID存储为表1中的文本,因此不允许我的搜索框找到它。我所要做的就是将它们全部转换为数字,并且修复了一切。

+0

认真吗?即使你明确地用它来调试你的问题,你会发布自己的答案,甚至不会给我点击一下。我的回答完全正确,说你在比较的细胞之间有一些差异。 – PGCodeRider

相关问题