扫描下一个条形码时取消突出显示单元格
Un-highlighting cells when next barcode is scanned
我一直在寻找一种方法来将条形码查找系统添加到 excel 上的某些库存 sheet。在这个论坛上,我发现了我正在寻找的确切解决方案。我一直在试图弄清楚如何添加一个功能,但是却一无所获(编码经验很少)。 sheet 跳转到相应的单元格并突出显示,这是必需的,因为它不会将单元格带到 sheet 视图的顶部,代码似乎只是确保所需的单元格是只要它在那里,就会显示在 sheet 上的任何位置。所以突出显示有助于快速找到页面上的正确单元格,但是当我去扫描并找到另一个条形码时,所有以前扫描的单元格都保持突出显示。到目前为止,我只是从主页选项卡中手动取消突出显示它们,但我想知道是否可以将某些内容添加到代码中以在我扫描下一个条形码时取消突出显示单元格?
此外,我正在使用 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
(以上代码的原始论坛来源:Jumping to an excel cell after reading a barcode to see if there is a match)
Before Picture of highlight
After picture of highlight
如果我没理解错的话,解决你的问题真的很简单:只需在你的主代码之前恢复颜色即可。
例如:
点击你的sheet激活它
运行 此代码用于检查颜色代码:
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
我一直在寻找一种方法来将条形码查找系统添加到 excel 上的某些库存 sheet。在这个论坛上,我发现了我正在寻找的确切解决方案。我一直在试图弄清楚如何添加一个功能,但是却一无所获(编码经验很少)。 sheet 跳转到相应的单元格并突出显示,这是必需的,因为它不会将单元格带到 sheet 视图的顶部,代码似乎只是确保所需的单元格是只要它在那里,就会显示在 sheet 上的任何位置。所以突出显示有助于快速找到页面上的正确单元格,但是当我去扫描并找到另一个条形码时,所有以前扫描的单元格都保持突出显示。到目前为止,我只是从主页选项卡中手动取消突出显示它们,但我想知道是否可以将某些内容添加到代码中以在我扫描下一个条形码时取消突出显示单元格? 此外,我正在使用 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
(以上代码的原始论坛来源:Jumping to an excel cell after reading a barcode to see if there is a match)
Before Picture of highlight
After picture of highlight
如果我没理解错的话,解决你的问题真的很简单:只需在你的主代码之前恢复颜色即可。
例如:
点击你的sheet激活它
运行 此代码用于检查颜色代码:
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