help with search if nothing then exit sub

buzz71023

Active Member
Joined
May 29, 2011
Messages
295
Office Version
  1. 2016
Platform
  1. Windows
I have a code that searches one page for a value entered into a inputbox. Everything is working great if the result is found; if it is not found or is left blank it turns all my shapes on the page one color (which is part of the code at the bottom). I would like to simply exit the sub if there is no any entries or if the entry is not found

below is my code:

Code:
Sub Bevel25_Click()
'SEARCH CODE
Dim myITEM As String, myRng As Range, NewLoc As String
Dim Found As Range, wrksht As Worksheet
Set wrksht = Sheets("List")
'Search for Product Code or Lot Number

myITEM = InputBox("Enter what you would like to search for.", "Search", "Enter Here")
Exit Sub

wrksht.Select

Set Found = Columns("C:D").Find(What:=myITEM, LookIn:=xlValues, lookat:=xlWhole)
If Found Is Nothing Then
    MsgBox "Not found"
Else
    Range("F" & Found.Row).Select
    
NewLoc = ActiveCell.Value

End If

Sheets("Rack Chart").Select
    ActiveSheet.Shapes(NewLoc).Select

'Turn Selection YELLOW
    With Selection.ShapeRange.Fill
            .Visible = msoTrue
            .ForeColor.RGB = RGB(255, 255, 0)
            .Transparency = 0
            .Solid
    End With
        
MsgBox ("The location of your item is " & NewLoc)
        
'Turn Selection back to BROWN
        With Selection.ShapeRange.Fill
            .Visible = msoTrue
            .ForeColor.RGB = RGB(153, 102, 51)
            .Transparency = 0
            .Solid
        End With

Sheets("Rack Chart").Range("a1").Select
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try like this

Code:
Set Found = Columns("C:D").Find(What:=myITEM, LookIn:=xlValues, lookat:=xlWhole)
If Found Is Nothing Then
    MsgBox "Not found"
    Exit Sub
Else
 
Upvote 0
it's not turning all my shapes brown anymore but now it's not finding my input from the inputbox. It actually doesnt appear to be doing anything aftre the inputbox goes away. I've made sure "debugger" is not running. I've exited out and reopened but nothing is happening after the input box entry

Code:
Sub Bevel25_Click()
'SEARCH CODE
Dim myITEM As String, myRng As Range, NewLoc As String
Dim Found As Range, wrksht As Worksheet

Set wrksht = Sheets("List")

'Search for Product Code or Lot Number

myITEM = InputBox("Enter what you would like to search for.", "Search", "Enter Here")
Exit Sub

wrksht.Select

Set Found = Columns("C:D").Find(What:=myITEM, LookIn:=xlValues, lookat:=xlWhole)
If Found Is Nothing Then
    MsgBox "Not found"
    Exit Sub
Else
    Range("F" & Found.Row).Select
    
NewLoc = ActiveCell.Value

End If

Sheets("Rack Chart").Select
    ActiveSheet.Shapes(NewLoc).Select

'Turn Selection YELLOW
    With Selection.ShapeRange.Fill
            .Visible = msoTrue
            .ForeColor.RGB = RGB(255, 255, 0)
            .Transparency = 0
            .Solid
    End With
        
MsgBox ("The location of your item is " & NewLoc)
        
'Turn Selection back to BROWN
        With Selection.ShapeRange.Fill
            .Visible = msoTrue
            .ForeColor.RGB = RGB(153, 102, 51)
            .Transparency = 0
            .Solid
        End With

Sheets("Rack Chart").Range("a1").Select
End Sub
 
Upvote 0
Nevermind. Something seem to be hanging it up. I retyped all of it and it works great now.. Thanks for the help! Much appreciated!
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,999
Members
452,373
Latest member
TimReeks

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