Macro help - search and if found, do

cdev19

New Member
Joined
Apr 12, 2009
Messages
8
Hi all

I am very stuck. Here's what I am trying to do:

- Sheet1 Cell D4 has a drop down list containing names.
- I need a macro to search through Sheet2 column D for that name
- IF found, then take some action (I have written code for this)
- ELSE (ie. if not found) do nothing.

I am a VBA beginner and can usually rely on searching this site to find something similar to what I require and tweak it to my needs. However, on this occasion I have searched and searched for an answer, but I always end up with code that LOOPS through all rows looking for the search string, and takes action for each row found. I don't know how to "remove" the LOOPS and just do it once, it always seems to ruin the code.

I just need the macro to find one instance in column D in order to take some action, no looping.

Any help would be greatly appreciated!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Code:
Sub findit()
   Dim Cell As Range
    Set Cell = Sheets("Sheet2").Range("D:D").Find("ab")
    If Not Cell Is Nothing Then MsgBox (Cell.Address)
       
End Sub
 
Upvote 0
or

<code>
Sub findvalue()
If Application.WorksheetFunction.CountIf(sheets("sheet2").Range("d:d"), sheets("sheet1").range("d4").value) >= 1 Then

MsgBox "found"
Else
MsgBox "not found"
End If

End Sub
<code>
 
Last edited:
Upvote 0
Makrini and matthew230 thank you both very much!

Works perfectly and replaces lines and lines of buggy code. :biggrin:
 
Upvote 0

Forum statistics

Threads
1,222,902
Messages
6,168,938
Members
452,227
Latest member
sam1121

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