Find a value in named range using VBA

insanity82007

Board Regular
Joined
Oct 10, 2007
Messages
130
How do I do this?

If the value exists in the named range I want to use an if statement to do something but I'm not sure how to find a value in the named range.

I tried this but it didn't work:

Code:
    For Each Value In Range("DropListCC")
        If Value = Sheet1.Range("e5").Value Then
            Exit For
        Else
            Sheet1.Range("e5").Value = ""
        End If
    Next
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
2 questions to start:

1. What is the value and what cell is it located in.

2. What do you want the IF statement to do?
 
Upvote 0
Hi insanity82007,

Try this, which is much more efficient then looping through each cell in your named range:

Code:
Option Explicit
Sub Macro4()

    'http://www.mrexcel.com/forum/showthread.php?642219-Find-a-value-in-named-range-using-VBA
    
    Dim rngFoundCell As Range
    
    With Range("DropListCC")
        Set rngFoundCell = .Find(What:=Sheets("Sheet1").Range("E5").Value, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
        If Not rngFoundCell Is Nothing Then
            'insanity82007, this is where you'd run code for the matching cell in your named range.
            'I've currently just returned the cell's address into a message box
            MsgBox rngFoundCell.Address
        Else
            MsgBox "There was no matching cell found in the ""DropListCC"" named range for the value in cell E5 of Sheet1."
        End If
    End With

End Sub

HTH

Robert
 
Upvote 0

Forum statistics

Threads
1,203,248
Messages
6,054,376
Members
444,721
Latest member
BAFRA77

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