我有一个工作簿,其中有4个不同的工作表用于库存目的(每个工作表对应一种类型的库存,以使组织更容易)。不是最好的设置,但我有一个条形码查找系统,我扫描商品的条形码,Excel会找到并突出显示相应的行(包含名称、图片、数量等信息)。这些都是手动更新的。最初所有的东西都在一张纸上,但最近我把它们分成了4张不同的纸。从那时起,条形码查找仅适用于原始库存表单(由于代码仅针对该表单,因此很有意义)。我一直无法弄清楚如何更改代码以在整个工作簿上工作。我尝试将工作表更改为工作簿(不起作用),然后尝试为每个工作表添加一个Set ws = ThisWorkbook.Sheets("") (也不起作用)和一些其他更改。如果任何人有任何想法如何改变它,使其搜索工作簿,而不是一张表,我将不胜感激。以下是第一个工作表的工作代码副本:
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提前感谢您的帮助。
发布于 2021-01-05 18:31:09
尝试以下代码:
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 Subhttps://stackoverflow.com/questions/65576759
复制相似问题