Warm tip: This article is reproduced from serverfault.com, please click

excel-将条码查找从工作表更改为工作簿

(excel - Change barcode lookup from sheet to workbook)

发布于 2021-01-05 10:05:12

我有一个工作簿,其中有 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

提前感谢你的帮助。

Questioner
intheend555
Viewed
0
ZygD 2021-01-05 20:13:17

试试这个代码:

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