我一直在寻找一种将条形码查找系统添加到 excel 上的某些库存表中的方法。在这个论坛上,我找到了我正在寻找的确切解决方案。我一直在试图弄清楚如何添加一个功能,但一无所获(编码经验很少(。工作表跳转到相应的单元格并突出显示,这是必需的,因为它不会将单元格带到工作表视图的顶部,代码似乎只是确保所需的单元格显示在工作表上的任何位置只要它在那里。因此,突出显示有助于快速在页面上找到正确的单元格,但是当我去扫描并找到另一个条形码时,所有先前扫描的单元格都保持突出显示。到目前为止,我只是从主页选项卡中手动取消突出显示它们,但我想知道是否可以在代码中添加一些东西以在扫描下一个条形码时取消突出显示单元格? 此外,我正在使用 excel 模板进行库存,如果需要重新排序,它会突出显示单元格行。不确定这增加了多少问题。
如果有人有任何想法,请提前感谢。
这是我在论坛上找到的代码,除了我只是更改了颜色值(一百行后亮黄色的海洋太多了(:
Private Sub CommandButton1_Click()
Dim code As Variant
Dim matchedCell As Range
code = InputBox("Please scan a barcode and hit enter if you need to")
Set matchedCell = Range("C2:C100").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
(以上代码的原始论坛来源:读取条形码后跳转到excel单元格以查看是否有匹配
(之前 突出显示的图片
高光后图片
如果我理解正确,解决您的问题真的很容易:只需在主代码之前恢复颜色即可。
例如:
-
单击工作表以激活它
-
运行此代码以检查颜色代码:
Sub getColor() MsgBox ActiveSheet.Range("C4").Interior.color End Sub
-
写下您的色号
-
修改宏代码
Private Sub CommandButton1_Click() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("Inventory List") Dim rangeToLook As Range Set rangeToLook = ws.Range("C2:C100") 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