Easy VBA need expert help: search

mrchonginhk

Well-known Member
Joined
Dec 3, 2004
Messages
679
I need to write a FUNCTION called

Finding(SearchValue As Variant,Rng As Range) As Boolean

which check if a particular value is located within that range.

Pls help.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Why not use the Find Method instead? It's already there for you to use. Are there restrictions on any of your inputs or anything like that?
 
Upvote 0
Public Function Finding(SearchValue As Variant, Rng As String) As Boolean
With Range(Rng)
Set c = .Find(SearchValue, LookIn:=xlValues, lookat:=xlWhole)
If Not c Is Nothing Then
Finding = True
Else
Finding = False
End If
End With
End Function


OR.......

Public Function Finding(SearchValue As Variant, Rng As String) As Boolean
If Application.WorksheetFunction.CountIf(Range(Rng), SearchValue) Then
Finding = True
Else
Finding = False
End If
End Function
 
Upvote 0
Here's version 3 ... :wink:

Public Function Finding(SearchValue As Variant, Rng As String) As Boolean
Finding = Application.WorksheetFunction.CountIf(Range(Rng), SearchValue)
End Function
 
Upvote 0
Version 4 ...

Public Function Finding(SearchValue As Variant, Rng As Range) As Boolean
Finding = Application.WorksheetFunction.CountIf(Rng, SearchValue)
End Function
 
Upvote 0
Test/demo script for Ver1,2,3 is ...

Public Sub testFinding()
MsgBox Finding("Hello", "A1:A20")
End Sub

Test/demo script for Ver 4 is ...

Public Sub testFinding()
MsgBox Finding("Hello", Range("A1:A20"))
End Sub
 
Upvote 0
The 1st version returns #Value
What could be wrong ?

I am trying the others.

My hat off!!

Thanks.
 
Upvote 0
Are you looking for a UDF ( ie. a user defined function that called from a cell ) or are you looking for a VBA function that is called from another VBA procedure ??
 
Upvote 0
The other version works !!! Thanks.

Is it I must put double quote like

=FINDING("ABC",Range) ??
Thanks.
 
Upvote 0

Forum statistics

Threads
1,203,453
Messages
6,055,530
Members
444,794
Latest member
HSAL

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