Less Specific Alphabetical Search Option

torweb

Board Regular
Joined
Dec 1, 2003
Messages
136
Is it possible to modify this code to peform a more gereral search...by first letter only.

Sub digitup()
Dim x As Variant
Set x = Cells.Find(What:=InputBox("Please enter your search criteria", "Search"), LookIn:=xlFormulas, LookAt:=xlPart)
If Not x Is Nothing Then
x.Activate
Else
MsgBox "Not found."
End If

End Sub

Thanks in advance!!!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
The following variation to your code using the Left() function should limit the search to the first character only.

Sub digitup()
Dim x As Variant
Set x = Cells.Find(What:=Left(InputBox("Please enter your search criteria", "Search"),1), LookIn:=xlFormulas, LookAt:=xlPart)
If Not x Is Nothing Then
x.Activate
Else
MsgBox "Not found."
End If

End Sub
 
Upvote 0
Okay, try this:

Sub digitup()
Dim x As Range
Dim Search As String
Search = InputBox("Please enter your search criteria", "Search")
For Each x In ActiveSheet.UsedRange
If x.Value Like Left(Search, 1) & "*" Then
x.Activate
GoTo Found
End If
Next x
MsgBox "Not found."
Exit Sub
Found:
End Sub

This doesn't use the Find method, but uses a loop instead. It only tries to match the first letter of the cell contents. I assumed you didn't really mean to search the formula as indicated in your previous post, since I believe this would always yield an equal sign, but rather the formula evaluation results.
 
Upvote 0
Try this:

Code:
Sub digitup()
    Dim x As Range
    Dim y, faddress As Variant
    With Sheets("sheet1").Cells
        y = InputBox("Please enter your search criteria")
        Set x = Cells.Find(What:=Left(y, 1), LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)
        If Not x Is Nothing Then
            faddress = x.Address
            If UCase(Left(x, 1)) <> UCase(Left(y, 1)) Then
                Do
                    Set x = .FindNext(x)
                Loop While Not x Is Nothing And x.Address <> faddress
            End If
            x.Activate
        Else
            MsgBox "Not found."
        End If
    End With
End Sub



EDIT: Damon is a faster coder than me! My understanding is the find is faster than the loop through each cell though. Guess the only thing that matters is it does what you want it to.

HTH
 
Upvote 0
Thanks both for the reply and ideas. Unfortunately, they both ran very slow and didn't seem to locate my search criteria (the letter B or A, or etc.). Also, if I replaced the LookIn:=xlFormulas with LookIn:=xlValues, my original code also ran very slow (we're searching through 6,200 entries).

I copied both codes into my sheet, ran them, and waited until they returnd from their searches....that's what happened...

Thanks very much again for the input!!!
 
Upvote 0
Just thinking of the obvious... If you know the column, did you already try autofilter? Using the Custom option you can pick from Begins With, Ends With, Contains, Does not Contain and so forth. Only get two criteria though...
 
Upvote 0

Forum statistics

Threads
1,224,315
Messages
6,177,843
Members
452,809
Latest member
mar_luna

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