VBA to pop up input box and search and select user defined string or number

Sandman1985

New Member
Joined
Jun 22, 2022
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Hope you're all well, I've been trying to get a macro button to make an input box pop up which allows me to enter in a text string or number to find and select on a very large sheet of data.
I know I can use Ctrl+F, but some of the people who need to use this will have their minds blown with that kind of change. SO I want to just put a large button which prompts them to enter what they are looking for.
I have the Input Box working, but can't get the find and select working.

Dim FindS As String
Dim Found As Boolean

FindS = InputBox("Enter the job number/name you want to search")

If FindS = "" Then
Exit Sub
End If

my code after this to find doesn't work.
have tried searching and appending my code with other code but failed.
Any help greatly appreciated.

Cheers,

Sandman
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi
your code doesn't search for anything , just if the inputbox is empty will exit from procdeure
 
Upvote 0
Hi
your code doesn't search for anything , just if the inputbox is empty will exit from procdeure
Hi,

Yes I only pasted the section that was working. I've been messing with it a bit.

Sub Search2()
Dim fnd As Range
Dim Ws As Worksheet

fnd = InputBox("Enter the job you want to search", "Job Number or Name")

For Each Ws In Worksheets
Set fnd = Ws.UsedRange.Find(InputBox, , xlValues, xlPart, , , False, , False)
If Not fnd Is Nothing Then
Ws.Activate
fnd.Select
Exit Sub
End If

End Sub

Still no good. Falling over in the range.
 
Upvote 0
try this
VBA Code:
Sub Search2()
Dim fnd As Variant
Dim Ws As Worksheet

fnd = InputBox("Enter the job you want to search", "Job Number or Name")

For Each Ws In Worksheets
Set fnd = Ws.UsedRange.Find(fnd, , xlValues, xlPart, , , False, , False)
If Not fnd Is Nothing Then
Ws.Activate
fnd.Select
Exit Sub
End If
Next
End Sub
 
Upvote 0
Hi,
see if this update to your code does what you want

VBA Code:
Sub Search()
    Dim fnd         As String
    Dim FoundCell   As Range
    Dim Ws          As Worksheet
    
    Do
        fnd = InputBox("Enter the job you want To search", "Job Number Or Name")
        'cancel pressed
        If StrPtr(fnd) = 0 Then Exit Sub
    Loop Until Len(fnd) > 0
    
    For Each Ws In Worksheets
        Set FoundCell = Ws.UsedRange.Find(fnd, , xlValues, xlPart, , , False, , False)
        If Not FoundCell Is Nothing Then
            Ws.Activate
            FoundCell.Select
            Exit Sub
        End If
    Next Ws
    
End Sub

Dave
 
Upvote 0
Solution
Hi,
see if this update to your code does what you want

VBA Code:
Sub Search()
    Dim fnd         As String
    Dim FoundCell   As Range
    Dim Ws          As Worksheet
   
    Do
        fnd = InputBox("Enter the job you want To search", "Job Number Or Name")
        'cancel pressed
        If StrPtr(fnd) = 0 Then Exit Sub
    Loop Until Len(fnd) > 0
   
    For Each Ws In Worksheets
        Set FoundCell = Ws.UsedRange.Find(fnd, , xlValues, xlPart, , , False, , False)
        If Not FoundCell Is Nothing Then
            Ws.Activate
            FoundCell.Select
            Exit Sub
        End If
    Next Ws
   
End Sub

Dave
That worked a treat. Thank you dmt32!
 
Upvote 0
try this
VBA Code:
Sub Search2()
Dim fnd As Variant
Dim Ws As Worksheet

fnd = InputBox("Enter the job you want to search", "Job Number or Name")

For Each Ws In Worksheets
Set fnd = Ws.UsedRange.Find(fnd, , xlValues, xlPart, , , False, , False)
If Not fnd Is Nothing Then
Ws.Activate
fnd.Select
Exit Sub
End If
Next
End Sub
That almost worked. The one below got it. Thanks for your help
 
Upvote 0
Glad we were able to resolve & appreciate feedback

You may like to add a msgbox to inform users if search value not found

VBA Code:
Sub Search()
    Dim fnd         As Variant
    Dim FoundCell   As Range
    Dim Ws          As Worksheet
   
    Do
        fnd = InputBox("Enter the job you want To search", "Job Number Or Name")
        'cancel pressed
        If StrPtr(fnd) = 0 Then Exit Sub
    Loop Until Len(fnd) > 0
   
    For Each Ws In Worksheets
        Set FoundCell = Ws.UsedRange.Find(fnd, , xlValues, xlPart, , , False, , False)
        If Not FoundCell Is Nothing Then
            Ws.Activate
            FoundCell.Select
            Exit Sub
        End If
    Next Ws
    MsgBox fnd & Chr(10) & "Record Not Found", 48, "Not Found"
End Sub

Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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