HOW can I alter this SEARCH Code

Mister H

Well-known Member
Joined
Mar 6, 2002
Messages
1,507
:)

Hi:

I am using the code below on a sheet in order to do a search. What can I add to it so that the code ONLY seraches in columns A C and D. I do not want to search any other columns.

Here is the code:

Code:
Private Sub CommandButton1_Click()
'Search
Dim Found As Range, tempcell As Range, Response As Integer, X
X = InputBox("Please enter the Text or Numbers you want to find", vbOKCancel)
Set Found = Cells.Find(what:=X)
If X = "" Then Exit Sub
If Found Is Nothing Then
    MsgBox X & " Not Found"
    Exit Sub
Else
    Application.Goto reference:=Found, Scroll:=True
End If
Response = MsgBox("Find Next Record", vbYesNo)
If Response = vbYes Then
    Do
        Set tempcell = Cells.FindNext(After:=Found)
        If Found.Row >= tempcell.Row And Found.Column >= tempcell.Column Then
            MsgBox "You are at the last record."
            Exit Do
        End If
        Set Found = tempcell
        Application.Goto reference:=Found, Scroll:=True
        Response = MsgBox("Find Next Record", vbYesNo)
        If Response = vbNo Then Exit Do
    Loop
    
End If
End Sub

THANKS to anyone that can assist.
Take Care,
Mark
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try changing
Set Found = Cells.Find(what:=X)
to
Set Found = Range("A:A, C:D").Find(what:=X)
 
Upvote 0
Hi jonmo1:

I tried and it still seems to be serching all columns of data. Here is what I changed to:

Code:
Private Sub CommandButton1_Click()
'Search
Dim Found As Range, tempcell As Range, Response As Integer, X
X = InputBox("Please enter the Text or Numbers you want to find", vbOKCancel)

'To Search Entire Sheet
'Set Found = Cells.Find(what:=X)
 
'Search ONLY columns A, C and D
[B]Set Found = Range("A:A, C:D").Find(what:=X)[/B]

Did I miss something?

As ALWAYS, THANKS for your help,
Mark
 
Upvote 0
You have to do it on BOTH the Find and the FindNext lines

Rich (BB code):
Private Sub CommandButton1_Click()
'Search
Dim Found As Range, tempcell As Range, Response As Integer, X
X = InputBox("Please enter the Text or Numbers you want to find", vbOKCancel)
Set Found = Range("A:A, C:D").Find(what:=X)
If X = "" Then Exit Sub
If Found Is Nothing Then
    MsgBox X & " Not Found"
    Exit Sub
Else
    Application.Goto reference:=Found, Scroll:=True
End If
Response = MsgBox("Find Next Record", vbYesNo)
If Response = vbYes Then
    Do
        Set tempcell = Range("A:A, C:D").FindNext(After:=Found)
        If Found.Row >= tempcell.Row And Found.Column >= tempcell.Column Then
            MsgBox "You are at the last record."
            Exit Do
        End If
        Set Found = tempcell
        Application.Goto reference:=Found, Scroll:=True
        Response = MsgBox("Find Next Record", vbYesNo)
        If Response = vbNo Then Exit Do
    Loop
 
End If
End Sub
 
Upvote 0
Oops!!! :oops: :oops: :oops:

THANKS jonmo1. I may have suffered some unknown brain trauma in my school days.

Have a GREAT day,
Mark

(y)
 
Upvote 0

Forum statistics

Threads
1,203,070
Messages
6,053,366
Members
444,658
Latest member
lhollingsworth

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