vba code


what would be the vba code to search for a string in a particular column and return true if a match is present?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

camper12Author Commented:
This is what I have

ElseIf (((Range("A17").Value = "A") Or Range("B17").Value = "C") And (Range("C17").Value = Columns("C1:C16").Select
    Set cell = Selection.Find(What:=Range("C17").Value, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)))
Anthony BerenguelCommented:
This should get you started
Function Find_Value_In_Column() As Boolean
    '// Searches for the value of cell C17 in the designated range
    '// and will stop searching once it finds a match.
    Dim returnValue As Boolean
    Dim rng As Range
    Dim c As Range
    Set rng = Range("A:A") '//change range as necessary
    For Each c In rng
        If c = Range("c17").Value Then
            returnValue = True
            Exit For
        End If
    Next c

    Find_Value_In_Column = returnValue
End Function

Open in new window

Roy CoxGroup Finance ManagerCommented:
It's not clear what ranges you are using. If you want to check if the value of C17 appears more than once in Range("C1:C17") then this should work

If Application.WorksheetFunction.CountIf(Range("C1:C17"), Range("C17")) > 0 Then MsgBox "True" 

Open in new window

Using CountIf as described above is probably the best option.
When using the value in cell C17 to search in column A you could also write the code like this:
strColumn = "A:A"
strSearch = Range("C17").Value
If Application.WorksheetFunction.CountIf(Range(strColumn), strSearch) > 0 Then
  MsgBox "Value " & strSearch & " found in column " & strColumn, vbOKOnly
  MsgBox "Value " & strSearch & " NOT found in column " & strColumn, vbOKOnly
End If

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.