使用绑定搜索excel中的数字



所以我对代码编写非常陌生,我试图在excel表格中搜索从条形码输入并在一行中找到的数字,然后选择另一行来输入我的现有数量条形码在g行,我想最终进入的行是F。这就是我所拥有的,但它不起作用。请注意,这是我第一次尝试编写代码。

Sub Barcodesearch()
'
' Barcodesearch Macro
'
' Keyboard Shortcut: Ctrl+Shift+B
'
Range("G3:G344").Select
Selection.Find What:= (Inputbox "Please scan a barcode and hit enter if needed"), After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range("F").Offset (0, 2)
End Sub

您需要处理没有匹配的情况:

Sub Barcodesearch()
Dim bc, f As Range, ws As Worksheet

Set ws = ActiveSheet

bc = Trim(InputBox("Please scan a barcode and hit enter if needed"))
If Len(bc) = 0 Then Exit Sub 'no entry was made
Set f = ws.Range("G3:G344").Find(What:=bc, LookIn:=xlFormulas, _
LookAt:=xlWhole, MatchCase:=False)
If Not f Is Nothing Then
f.Offset(0, -1).Select                   'got a match
Else
MsgBox "Barcode '" & bc & "' not found"  'no match
End If
End Sub

最新更新