my barcode search wont find my barcode in my worksheet. my vba search code is not working properly












1















When I scan my item bar code in my input field (megBox) my for loop does not Find/Match my item.



It keeps saying "item not found".



It only finds this item(21603000815) and prints "Found value on row 1". (which in only one of the many items)



everything else returns:"item not found"



how can it find the other items?



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









share|improve this question




















  • 2





    Despite your question I recommend to read and apply How to avoid using Select in Excel VBA to your code.

    – Pᴇʜ
    Nov 13 '18 at 15:43











  • On first glance - ignoring not declared variables your For q loop looks at the first cell in the loop (B1). If the value is found it says so and exits the loop, if it's not the value then a messagebox is displayed and the procedure exits. Just removing that Exit Sub would cause the loop to carry on.

    – Darren Bartrup-Cook
    Nov 13 '18 at 16:32











  • Could you delete your original question as well please - can't vote to close it as it's already on hold.

    – Darren Bartrup-Cook
    Nov 13 '18 at 17:00
















1















When I scan my item bar code in my input field (megBox) my for loop does not Find/Match my item.



It keeps saying "item not found".



It only finds this item(21603000815) and prints "Found value on row 1". (which in only one of the many items)



everything else returns:"item not found"



how can it find the other items?



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









share|improve this question




















  • 2





    Despite your question I recommend to read and apply How to avoid using Select in Excel VBA to your code.

    – Pᴇʜ
    Nov 13 '18 at 15:43











  • On first glance - ignoring not declared variables your For q loop looks at the first cell in the loop (B1). If the value is found it says so and exits the loop, if it's not the value then a messagebox is displayed and the procedure exits. Just removing that Exit Sub would cause the loop to carry on.

    – Darren Bartrup-Cook
    Nov 13 '18 at 16:32











  • Could you delete your original question as well please - can't vote to close it as it's already on hold.

    – Darren Bartrup-Cook
    Nov 13 '18 at 17:00














1












1








1








When I scan my item bar code in my input field (megBox) my for loop does not Find/Match my item.



It keeps saying "item not found".



It only finds this item(21603000815) and prints "Found value on row 1". (which in only one of the many items)



everything else returns:"item not found"



how can it find the other items?



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









share|improve this question
















When I scan my item bar code in my input field (megBox) my for loop does not Find/Match my item.



It keeps saying "item not found".



It only finds this item(21603000815) and prints "Found value on row 1". (which in only one of the many items)



everything else returns:"item not found"



how can it find the other items?



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






excel vba excel-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 15:41









Pᴇʜ

21.8k42750




21.8k42750










asked Nov 13 '18 at 15:40









Dermot JohnsonDermot Johnson

546




546








  • 2





    Despite your question I recommend to read and apply How to avoid using Select in Excel VBA to your code.

    – Pᴇʜ
    Nov 13 '18 at 15:43











  • On first glance - ignoring not declared variables your For q loop looks at the first cell in the loop (B1). If the value is found it says so and exits the loop, if it's not the value then a messagebox is displayed and the procedure exits. Just removing that Exit Sub would cause the loop to carry on.

    – Darren Bartrup-Cook
    Nov 13 '18 at 16:32











  • Could you delete your original question as well please - can't vote to close it as it's already on hold.

    – Darren Bartrup-Cook
    Nov 13 '18 at 17:00














  • 2





    Despite your question I recommend to read and apply How to avoid using Select in Excel VBA to your code.

    – Pᴇʜ
    Nov 13 '18 at 15:43











  • On first glance - ignoring not declared variables your For q loop looks at the first cell in the loop (B1). If the value is found it says so and exits the loop, if it's not the value then a messagebox is displayed and the procedure exits. Just removing that Exit Sub would cause the loop to carry on.

    – Darren Bartrup-Cook
    Nov 13 '18 at 16:32











  • Could you delete your original question as well please - can't vote to close it as it's already on hold.

    – Darren Bartrup-Cook
    Nov 13 '18 at 17:00








2




2





Despite your question I recommend to read and apply How to avoid using Select in Excel VBA to your code.

– Pᴇʜ
Nov 13 '18 at 15:43





Despite your question I recommend to read and apply How to avoid using Select in Excel VBA to your code.

– Pᴇʜ
Nov 13 '18 at 15:43













On first glance - ignoring not declared variables your For q loop looks at the first cell in the loop (B1). If the value is found it says so and exits the loop, if it's not the value then a messagebox is displayed and the procedure exits. Just removing that Exit Sub would cause the loop to carry on.

– Darren Bartrup-Cook
Nov 13 '18 at 16:32





On first glance - ignoring not declared variables your For q loop looks at the first cell in the loop (B1). If the value is found it says so and exits the loop, if it's not the value then a messagebox is displayed and the procedure exits. Just removing that Exit Sub would cause the loop to carry on.

– Darren Bartrup-Cook
Nov 13 '18 at 16:32













Could you delete your original question as well please - can't vote to close it as it's already on hold.

– Darren Bartrup-Cook
Nov 13 '18 at 17:00





Could you delete your original question as well please - can't vote to close it as it's already on hold.

– Darren Bartrup-Cook
Nov 13 '18 at 17:00












1 Answer
1






active

oldest

votes


















0














I'm not 100% sure what all your code is meant to be doing. I hope the code below mirrors what you're expecting fairly closely.



You'll have to change the sheet names I've used and maybe change the last bit?



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





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284526%2fmy-barcode-search-wont-find-my-barcode-in-my-worksheet-my-vba-search-code-is-no%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I'm not 100% sure what all your code is meant to be doing. I hope the code below mirrors what you're expecting fairly closely.



    You'll have to change the sheet names I've used and maybe change the last bit?



    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





    share|improve this answer




























      0














      I'm not 100% sure what all your code is meant to be doing. I hope the code below mirrors what you're expecting fairly closely.



      You'll have to change the sheet names I've used and maybe change the last bit?



      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





      share|improve this answer


























        0












        0








        0







        I'm not 100% sure what all your code is meant to be doing. I hope the code below mirrors what you're expecting fairly closely.



        You'll have to change the sheet names I've used and maybe change the last bit?



        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





        share|improve this answer













        I'm not 100% sure what all your code is meant to be doing. I hope the code below mirrors what you're expecting fairly closely.



        You'll have to change the sheet names I've used and maybe change the last bit?



        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 16:58









        Darren Bartrup-CookDarren Bartrup-Cook

        13.9k11432




        13.9k11432






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284526%2fmy-barcode-search-wont-find-my-barcode-in-my-worksheet-my-vba-search-code-is-no%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation