2017-07-27 80 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

相關問題