мой поиск штрих-кода не найдет мой штрих-код в моем листе.мой код поиска VBA не работает должным образом - PullRequest
0 голосов
/ 13 ноября 2018

Когда я сканирую свой штрих-код элемента в поле ввода (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 голосов
/ 13 ноября 2018

Я не уверен на 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...