将条形码查找从 sheet 更改为工作簿
Change barcode lookup from sheet to workbook
我有一个工作簿,其中有 4 个不同的 sheet 用于库存用途(每个 sheet 对应一种库存类型,以便于组织)。不是最好的设置,但我有一个条形码查找系统,我在其中扫描项目的条形码,Excel 找到并突出显示相应的行(包含名称、图片、数量等信息),这些都是手动更新的。最初所有内容都在一个 sheet 上,但最近我将它们分类为 4 个不同的 sheet。从那时起,条形码查找仅适用于原始库存 sheet(这是有道理的,因为代码仅适用于那个 sheet)。我一直无法弄清楚如何更改代码以在整个工作簿上工作。我尝试将 worksheet 更改为工作簿(没有用),然后我尝试为每个 sheet 添加一个 Set ws = ThisWorkbook.Sheets("")
(也没有用)和其他一些变化。如果有人知道如何更改它以便搜索工作簿而不是 sheet,我将不胜感激。
这是第一个 sheet:
的工作代码副本
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Inventory List")
Dim rangeToLook As Range
Set rangeToLook = ws.Range("C3:C1000")
Dim wholeRange As Range
Set wholeRange = rangeToLook.Resize(, 10)
' change 14408667 to yours grey color code here
wholeRange.Cells.Interior.Color = 14408667
Dim code As Variant
code = InputBox("Please scan a barcode and hit enter if you need to")
Dim matchedCell As Range
Set matchedCell = rangeToLook.Find(what:=code, LookIn:=xlValues, _
lookat:=xlWhole, MatchCase:=True)
If Not matchedCell Is Nothing Then
With matchedCell
Application.Goto .Cells(1)
.Resize(1, 10).Interior.ColorIndex = 20
End With
Else
MsgBox "Barcode Not Found"
End If
End Sub
提前感谢您的帮助。
试试这个代码:
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim rangeToLook As Range
Dim wholeRange As Range
Dim code As Variant
Dim matchedCell As Range
code = InputBox("Please scan a barcode and hit enter if you need to")
For Each ws In ThisWorkbook.Sheets
Set rangeToLook = ws.Range("C3:C1000")
Set wholeRange = rangeToLook.Resize(, 10)
wholeRange.Interior.Color = 14408667
Set matchedCell = rangeToLook.Find(what:=code, LookIn:=xlValues, _
lookat:=xlWhole, MatchCase:=True)
If Not matchedCell Is Nothing Then
With matchedCell
Application.Goto .Cells(1)
.Resize(1, 10).Interior.ColorIndex = 20
Exit For
End With
End If
Next
If matchedCell Is Nothing Then
MsgBox "Barcode Not Found"
End If
End Sub
我有一个工作簿,其中有 4 个不同的 sheet 用于库存用途(每个 sheet 对应一种库存类型,以便于组织)。不是最好的设置,但我有一个条形码查找系统,我在其中扫描项目的条形码,Excel 找到并突出显示相应的行(包含名称、图片、数量等信息),这些都是手动更新的。最初所有内容都在一个 sheet 上,但最近我将它们分类为 4 个不同的 sheet。从那时起,条形码查找仅适用于原始库存 sheet(这是有道理的,因为代码仅适用于那个 sheet)。我一直无法弄清楚如何更改代码以在整个工作簿上工作。我尝试将 worksheet 更改为工作簿(没有用),然后我尝试为每个 sheet 添加一个 Set ws = ThisWorkbook.Sheets("")
(也没有用)和其他一些变化。如果有人知道如何更改它以便搜索工作簿而不是 sheet,我将不胜感激。
这是第一个 sheet:
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Inventory List")
Dim rangeToLook As Range
Set rangeToLook = ws.Range("C3:C1000")
Dim wholeRange As Range
Set wholeRange = rangeToLook.Resize(, 10)
' change 14408667 to yours grey color code here
wholeRange.Cells.Interior.Color = 14408667
Dim code As Variant
code = InputBox("Please scan a barcode and hit enter if you need to")
Dim matchedCell As Range
Set matchedCell = rangeToLook.Find(what:=code, LookIn:=xlValues, _
lookat:=xlWhole, MatchCase:=True)
If Not matchedCell Is Nothing Then
With matchedCell
Application.Goto .Cells(1)
.Resize(1, 10).Interior.ColorIndex = 20
End With
Else
MsgBox "Barcode Not Found"
End If
End Sub
提前感谢您的帮助。
试试这个代码:
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim rangeToLook As Range
Dim wholeRange As Range
Dim code As Variant
Dim matchedCell As Range
code = InputBox("Please scan a barcode and hit enter if you need to")
For Each ws In ThisWorkbook.Sheets
Set rangeToLook = ws.Range("C3:C1000")
Set wholeRange = rangeToLook.Resize(, 10)
wholeRange.Interior.Color = 14408667
Set matchedCell = rangeToLook.Find(what:=code, LookIn:=xlValues, _
lookat:=xlWhole, MatchCase:=True)
If Not matchedCell Is Nothing Then
With matchedCell
Application.Goto .Cells(1)
.Resize(1, 10).Interior.ColorIndex = 20
Exit For
End With
End If
Next
If matchedCell Is Nothing Then
MsgBox "Barcode Not Found"
End If
End Sub