How do I Limit a VBA Find Command to a single column?

Lidsavr

Active Member
Joined
Jan 10, 2008
Messages
330
I am running Excel 2007 and writing code that 2003 users will run.

I have the following code looking for any hyphens (-) in Column E. If it doesn't find any, it automatically starts looking at other columns. How can I limit the search to only column E?

Here is the code that I am using

Code:
    Range("E:E").Select
    Cells.Find(What:="-", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate

Thank you for your help in advance.

Charles
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try:
Code:
Range("E:E").Find(What:="-", After:=ActiveCell, LookIn:=xlFormulas, _
  LookAt:= xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
  MatchCase:= False, SearchFormat:=False).Activate
Your code is Cells.Find which means even if Column E is selected, the code is then searching through all the Cells (i.e. the whole worksheet)
 
Upvote 0
JackDanIce,
Thank you. I understand what you mean now that I have seen your code. It works.

I have a question though. If it does not find anything, I want the code to Exit.

Do you know how to do that?
 
Upvote 0
Try:
Code:
On Error Goto notfound
Range("E:E").Find(What:="-", After:=ActiveCell, LookIn:=xlFormulas, _
  LookAt:= xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
  MatchCase:= False, SearchFormat:=False).Activate


notfound:
Msgbox "- is not found in column E, macro stopping"
Exit Sub
 
Upvote 0
JackDanIce,
Thank you. The code is working perfectly now!

I've have and continue to learn much from people like you on this forum. Thank you for sharing your knowledge.

Charles
 
Upvote 0
You're welcome, glad I could help, thanks for the feedback too.
 
Upvote 0
There is another way to do that that doesn't involve GOTO.
Drop the Activate, assign it to a range and see if it returned anything. Then utilize the range variable to do what you want.

Code:
Sub test()
Dim c As Range
Set c = Range("E:E").Find(What:="-", After:=ActiveCell, LookIn:=xlFormulas, _
  LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
  MatchCase:=False)
If c Is Nothing Then
    MsgBox "Not Found"
    Exit Sub
End If
c.Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,786
Members
452,942
Latest member
VijayNewtoExcel

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