Excel VBA: Input Box returning "SN Found" or "SN Not Found"

Bluesguy07

New Member
Joined
Mar 11, 2016
Messages
28
Hello Guys and Gals,

I found this macro elsewhere, and modified it slightly to fit my needs. However, I'm unsure how to turn it essentially into an IF statement where it returns "SN Found" if it does find the serial number or "SN Not Found" if it does not find the serial number. I would also like to add something that will actually copy the serial number if found into column C (starting with C2) and if not found into column D (D2). Any assistance would be greatly appreciated.

*Note* - This is a 1 page workbook, so multiple worksheets are not needed for this

Code:
Sub SNLookup()Dim x As Long
Dim lr, lookRng As Range
Dim findStr As String
Dim foundCell As Variant




lr = Cells(Rows.Count, "A").End(xlUp).Row
Set lookRng = Range("A1:A" & lr)


findStr = InputBox("Enter Serial Number")


    For x = 1 To lr
        If Range("A" & x).Value = findStr Then
        Set foundCell = Range("A" & x).Offset(0, 1)
        MsgBox foundCell & "SN Found"
        End If
    Next x


End Sub

Just as an aside, I am not even sure that all of the code I referenced is needed for this task.
 
Last edited:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
How about
Code:
Sub SNLookup()
   Dim Fnd As Range
   Dim findStr As String

   findStr = InputBox("Enter Serial Number")
   Set Fnd = Range("A:A").find(findStr, , , xlWhole, , , False, , False)
   If Not Fnd Is Nothing Then
      Range("C" & Rows.Count).End(xlUp).Offset(1).Value = Fnd.Value
   Else
       Range("D" & Rows.Count).End(xlUp).Offset(1).Value = Fnd.Value
   End If
End Sub
 
Upvote 0
It sort of work, and mostly doesn't work.

I need text box stating whether or not the SN was found. It does copy the value if found into column C, but it returns an error "Object variable or With block variable not set" if I enter a SN that does not exist.

I just realized that your code doesn't actually have anything that will actually display a message box after finding or not finding the SN. I'm heading to a meeting and I'll see if I can't figure out how to get that added.

Regardless, if it doesn't find the SN it does not paste the entered SN into column D.
 
Last edited:
Upvote 0
Hello, I think this achieves what you want.

Code:
Sub SNLookup()


Dim lr
Dim findStr As String
Dim rFind As Range


lr = Cells(Rows.Count, "A").End(xlUp).Row


findStr = InputBox("Enter Serial Number")
    
    Set rFind = Range("A1:A" & lr).Find(findStr)


    If rFind Is Nothing Then
        'serial number not found
        MsgBox "SN Not found"
        Cells(Range("D" & Rows.Count).End(xlUp).Row + 1, "D") = findStr
    Else
        'serial number found
        MsgBox "SN Found"
        Cells(Range("C" & Rows.Count).End(xlUp).Row + 1, "C") = findStr
    End If
    
    'housekeeping
    Set rFind = Nothing


End Sub
 
Upvote 0
Ok, How about
Code:
Sub SNLookup()
   Dim Fnd As Range
   Dim findStr As String

   findStr = InputBox("Enter Serial Number")
   Set Fnd = Range("A:A").find(findStr, , , xlWhole, , , False, , False)
   If Not Fnd Is Nothing Then
      MsgBox "Found"
      Range("C" & Rows.Count).End(xlUp).Offset(1).Value = Fnd.Value
   Else
      MsgBox "Not Found"
      Range("D" & Rows.Count).End(xlUp).Offset(1).Value = findStr
   End If
End Sub
 
Upvote 0
Ok, How about
Code:
Sub SNLookup()
   Dim Fnd As Range
   Dim findStr As String

   findStr = InputBox("Enter Serial Number")
   Set Fnd = Range("A:A").find(findStr, , , xlWhole, , , False, , False)
   If Not Fnd Is Nothing Then
      MsgBox "Found"
      Range("C" & Rows.Count).End(xlUp).Offset(1).Value = Fnd.Value
   Else
      MsgBox "Not Found"
      Range("D" & Rows.Count).End(xlUp).Offset(1).Value = findStr
   End If
End Sub

This works beautifully. Thank you very much.

Gallen,

Since Fluff's worked, I did not give yours a try. I can try it later though just to let you know if it worked if you'd like.



You guys are the true heroes of the world.
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
So, it appears that a loop is needed, and the person I'm doing this for doesn't need to show whether or not the SN is found or not found since I have it split into multiple columns. However, he needs it on a loop. I created the below, but with this it does not allow for the macro to be cancelled with the "Cancel" button on the input box. Any idea how to fix that?

Code:
Sub SNLookup()   Dim Fnd As Range
   Dim findStr As String
   Dim i As Integer
For i = 1 To 200
 
   findStr = InputBox("Enter Serial Number")
   Set Fnd = Range("A:A").Find(findStr, , , xlWhole, , , False, , False)
   If Not Fnd Is Nothing Then
      Range("C" & Rows.Count).End(xlUp).Offset(1).Value = Fnd.Value
   Else
      Range("D" & Rows.Count).End(xlUp).Offset(1).Value = findStr
   End If
   
Next i
  
End Sub
 
Last edited:
Upvote 0
Try
Code:
Sub SNLookup()
Dim Fnd As Range
   Dim findStr As String
   Dim i As Integer
For i = 1 To 200
 
   findStr = InputBox("Enter Serial Number")
   If findStr = "" Then Exit Sub
   Set Fnd = Range("A:A").find(findStr, , , xlWhole, , , False, , False)
   If Not Fnd Is Nothing Then
      Range("C" & Rows.Count).End(xlUp).Offset(1).Value = Fnd.Value
   Else
      Range("D" & Rows.Count).End(xlUp).Offset(1).Value = findStr
   End If
   
Next i
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top