我的条形码搜索无法在我的工作表中找到我的条形码。我的vba搜索代码无法正常工作

时间:2018-11-13 15:40:44

标签: excel vba excel-vba

当我在输入字段(megBox)中扫描商品条形码时,for循环不会查找/匹配我的商品。

它总是说“找不到项目”。

它仅找到此项目(21603000815)并打印“第1行的发现值”。 (其中仅一项)

其他所有内容都会返回:“找不到商品”

如何找到其他物品?

Sub findIt()
    Dim i As Long
    Dim x As Integer, y As Integer, q As Integer
    Worksheets(3).Activate
    With Worksheets(3).Range("a1:d12") 'looking in sheet with all items
        x = 0
        Do While xforms <> -1
            xforms = Application.InputBox("Enter Barcode", xTitleId, "", Type:=1)
            Application.Visible = True

            For q = 1 To 500    ' Revise the 500 to include all of your values
                If Worksheets(3).Cells(q, 2).Value = xforms Or Worksheets(3).Cells(q, 2).Formula = xforms Then
                    MsgBox ("Found value on row " & q)
                    Application.Visible = True
                    GoTo skip
                Else
                    MsgBox ("item Not Found")
                    Exit Sub
                End If
            Next q
    skip:
            ' This MsgBox will only show if the loop completes with no success
            MsgBox ("yessssssssssss")


            Worksheets("Barcodes").Range("a1").Offset(y, 0).Value = xforms 'putting items/barcord in a seperate tad to check for dupliate items
            Set c = .Find(xforms, LookIn:=xlValues)
            c.Select
            i = ActiveCell.Row
            Rows(i).Select
            Selection.Copy Worksheets("Shop Lable Info").Range("a1").Offset(x, 0)
            Rows(i + 1).Select
            Selection.Copy Worksheets("Shop Lable Info").Range("a2").Offset(x, 0)
            x = x + 2
            y = y + 1
        Loop
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

我不确定100%您的所有代码打算做什么。我希望下面的代码能够非常接近您的期望。

您必须更改我使用的工作表名称,也许还要更改最后一位?

Public Sub FindIt()

    Dim xForms As Long
    Dim rSearch As Range
    Dim rFound As Range
    Dim sFirstAdd As String
    Dim rCopyRange As Range

    Dim xTitleID As String: xTitleID = "Title for InputBox"

    xForms = Application.InputBox("Enter Barcode", xTitleID, "", Type:=1)

    'Only continue if a number > 0 was entered in xForms.
    'Pressing Cancel sets xForms to 0.
    If xForms <> 0 Then

        'ThisWorkbook is the file containing this code.
        With ThisWorkbook.Worksheets("Sheet3")
            Set rSearch = .Range(.Cells(1, 2), .Cells(.Rows.Count, 2).End(xlUp)) 'Row B1:B500 in your code.
        End With

        'Look for the first occurrence.
        Set rFound = rSearch.Find(xForms, rSearch.Cells(1, 1), xlValues, xlWhole, , xlNext)

        If Not rFound Is Nothing Then
            Set rCopyRange = rFound
            sFirstAdd = rFound.Address
            'If a value was found then search for others.
            'Stop when the search wraps back to the top again.
            Do
                Set rFound = rSearch.FindNext(rFound)
                Set rCopyRange = Union(rCopyRange, rFound) 'Create a range from all the found values.
            Loop While rFound.Address <> sFirstAdd

            'Copy the found rows to the "Bar Codes" sheet.
            With ThisWorkbook.Worksheets("Bar Codes")
                rCopyRange.EntireRow.Copy Destination:=.Cells(.Rows.Count, 1).End(xlUp)
            End With

        End If
    End If

End Sub