0

I have some code here using an inputbox to get some data. I want the inputbox to loop until user closes it. But when I close the inputbox, I get my message popup that says "Serial Number Already Exists". I'm not sure what I'm missing to not get that to popup when closing the inputbox.

Private Sub OptionButton1_Click()
    'Outgoing

    Dim ws As Worksheet
    Set ws = Worksheets("CRR")
    Dim OutPO As String
    Dim Outgoing

    OutPO = InputBox("Enter Outgoing PO Number", "PO")

    If OutPO = "" Then
        MsgBox "Enter PO Before Scanning", vbCritical, ""
        Exit Sub
    End If

    Do
        Outgoing = InputBox("Enter Outgoing CCA Serial Number!", "Outgoing")

        With Sheets("CRR").Range("B:B")
            Set Rng = .Find(What:=Outgoing, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)

            If Not Rng Is Nothing Then
                MsgBox "Serial Number Already Exists!", vbExclamation, "Error"
            Else
                Dim lRow As Long
                lRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Offset(1).Row

                ws.Cells(lRow, 1).Value = OutPO
                ws.Cells(lRow, 2).Value = Outgoing
                ws.Cells(lRow, 3).Value = Date
                ws.Cells(lRow, 4).Value = Environ("Username")
            End If
        End With

    Loop Until Len(Outgoing) = 0 
End Sub
GSerg
  • 76,472
  • 17
  • 159
  • 346
Noob2Java
  • 213
  • 1
  • 7
  • 18
  • 4
    `If StrPtr(Outgoing) = 0 Then Exit Do` after the `= InputBox()`? – GSerg Mar 02 '20 at 18:54
  • 1
    Another thing to consider is making your own form that has the functionality built in to it. This would avoid using multiple inputbox, you could see all text input fields at the same time and closing it would have no side effects. – HackSlash Mar 02 '20 at 19:06
  • This means that your `Outgoing` was not find in `Sheets("CRR").Range("B:B")` and `Rng Is Nothing`... Are you sure that your string to be found exists in that range? Please test in a separate code your Find function for the word you use and see what it returns. – FaneDuru Mar 02 '20 at 19:10
  • @GSerg Thanks for your help. I placed that after my Outgoing InputBox but it still fires. – Noob2Java Mar 02 '20 at 19:11
  • 1
    Then apparently you are not really [pressing Cancel](https://stackoverflow.com/a/20909528/11683). Try `If Len(Outgoing) = 0` like you have in `Until`. – GSerg Mar 02 '20 at 19:14
  • Thanks @GSerg that did the trick. – Noob2Java Mar 02 '20 at 19:16

0 Answers0