使用条形码扫描仪通过 Visual Basic for Applications 搜索和编辑电子表格



尝试使用条形码扫描仪自动搜索电子表格并检查它(资产的例行验证(。

我录制了宏,它搜索一个特定的字符串,然后将单元格颜色更改为"选中它"。

如何让它等待来自扫描仪的输入,然后搜索该输入?我希望"IT2000"是一个根据条形码扫描仪而变化的变量。

Recored Macro:

Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+b
'
Sheets("S15-137").Select
Cells.Find(What:="IT2000", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Selection.Style = "Good"
End Sub

尝试过此操作,扫描程序输入现在可以工作,但代码失败。另外,如何使其默认搜索所有工作表?

Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+b
'
Dim scannerInput As String
Dim cellFound As Range
scannerInput = Application.InputBox("Scan barcode")
Set cellFound = ThisWorkbook.Sheets("S15-137").Cells.Find(What:=Barcode, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not cellFound Is Nothing Then
cellFound.Style = "Good"
Else
MsgBox "Value not found"
End If
End Sub

据我所知,您可以让条形码扫描仪将刚刚扫描的值插入Excel工作表中。如果你能做到这一点,那么你就可以编写一个Worksheet_Change事件。添加一个空工作表,扫描仪将在其中输出条形码,并在其工作表模块中插入以下代码:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Barcode As String
Dim FindCell As Range
Barcode = Target.Value
Set FindCell = ThisWorkbook.Sheets("S15-137").Cells.Find(What:=Barcode, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not FindCell Is Nothing Then
FindCell.Style = "Good"
End If
End Sub

最新更新