VBA to search Excel by Field Input Values

SanjayGMusafir

Well-known Member
Joined
Sep 7, 2018
Messages
1,450
Office Version
  1. 2021
Platform
  1. MacOS
Hi Experts
I have fiel where I put 10 to 12 digit numbers. The Column is formatted as "##### #####" to give a space to make number readable like in India.

If I input "1234567890" it shows me "12345 67890"

Now I'm using a VBA that searches and return from xLValues. It works perfect if I search for an thing in output format. for eg. "12345 67890"

Just now got in a situation where I need to search by "1234567890" and it should take me to the cell where "12345 67890" is displayed.

Any help is highly appreciated.

Thanks a lot

This is the complete code I'm using right now -

VBA Code:
Sub SearchSheet()
'
' SearchSheet Macro
'

'
    Dim xRng As Range
    
    x = Application.InputBox("", "Search for...")
    
    'To reset Zoom levels of the sheet
    Call ZoomReset
    
    Application.ScreenUpdating = True
    Range("A1").Select
    
    If x = "" Then 'if Search Input is Blank
    
        MsgBox "Blank Search..."
        Exit Sub
    
    ElseIf x = False Then 'If Search is Cancelled
        
        MsgBox "You Cancelled..."
        Exit Sub
            
    Else
        Set xRng = Cells.Find(What:=x, After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False)
        
        If xRng Is Nothing Then 'If No Result Found
    
            MsgBox "No Search Result Found..."
            Exit Sub
                
        Else
            
            xRng.Select 'When Result is Found
            
        End If
    End If
    
End Sub

I experimented changing a part of FIND from "xlValues" to "xlFormulas" but it didn't work. Probably, I'm missing some trick.

VBA Code:
Set xRng = Cells.Find(What:=x, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False)
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Before your Set xRng = line, put this:
VBA Code:
x = Format(x, "##### #####")
and see if that makes a difference.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,076
Messages
6,122,988
Members
449,093
Latest member
Mr Hughes

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