Excel Textbox仅从条形码扫描仪接收一个字符
扫描条形码时,文本框中只输入第一个数字。文本框的任何更改都会激活该代码。第二个问题是在搜索完成后将焦点放在文本框上,以便进行下一次扫描Excel Textbox仅从条形码扫描仪接收一个字符,excel,vba,Excel,Vba,扫描条形码时,文本框中只输入第一个数字。文本框的任何更改都会激活该代码。第二个问题是在搜索完成后将焦点放在文本框上,以便进行下一次扫描 Private Sub Barcode_TxtBox_Change() If Barcode_TxtBox.Value = "" Then ' set focus back to textbox Barcode_TxtBox.Activate Else ' If match found then populate the box next to the
Private Sub Barcode_TxtBox_Change()
If Barcode_TxtBox.Value = "" Then
' set focus back to textbox
Barcode_TxtBox.Activate
Else
' If match found then populate the box next to the code found with X
If WorksheetFunction.CountIf(Sheets("Sheet1").Columns(2), Barcode_TxtBox.Value) = 1 Then
Set TargetCell = Sheets("Sheet1").Columns(2).Find(Barcode_TxtBox.Value, , xlValues, xlWhole).Offset(0, 1)
TargetCell.Value = "X"
Else
MsgBox "Code not found"
End If
' Clear textbox and set focus back to textbox for next scan
Barcode_TxtBox.Value = ""
Barcode_TxtBox.Activate
End If
End Sub
扫描完成后,条形码的第一个字符被输入文本框,没有找到匹配项,因此激活MSGBox“Code not found”(未找到代码)条形码扫描仪倾向于发送一个“换行符”字符作为扫描值的结尾,因此您需要等待该字符出现,然后再执行值的查找。注释掉
Else
块中的代码,并将其替换为Debug.Print Barcode_TxtBox.Value
并查看即时窗格中的输出-这应该会有所帮助扫描仪本质上是一个带光学阅读器的键盘,而不是按键-它一次发送一个字符不会让我感到惊讶,而Change
处理程序将为它们中的每一个运行。您希望代码路径能够理解输入是部分的/不完整的。至于焦点问题,请尝试。SetFocus