excel - 将条形码查找从工作表更改为工作簿
问题描述
我有一个工作簿,其中有 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
提前感谢您的帮助。
解决方案
试试这个代码:
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
推荐阅读
- python - 如何根据列值自定义数据框的顺序
- c - 在函数中使用结构变量 - 冲突类型
- r - 根据单元格值 (R) 在矩阵中查找路径
- python - 从类中获取价值到另一个 Python
- javascript - 如何使用 Javascript 滚动到元素?
- angular - 带有本地更改检测的 Andular 材料日期选择器问题
- powershell - 从另一个 PS Session 调用进程的私有环境变量
- ruby-on-rails - 我已经在作者(has_many)和书籍(belongs_to)的rails中创建了一个关联,现在删除作者(依赖::destroy)后,我得到了异常
- sql - 为什么我在 SQL Server 'in' 子句中出现语法错误?
- flutter - 为什么在颤动中点击通知时路由不起作用?(项目基于GetX和flutter本地推送通知包)