Solved

Constructing a Range to use in Find Method Excel VBA

Posted on 2011-03-08
4
323 Views
Last Modified: 2012-05-11
I have created a generic function in VBA which uses the find method to search a range:

   
Public Function fCheckValueExists(ws As Worksheet, rng As Variant, _
    strValue As String) As Variant

    fCheckValueExists = ws.Range(rng).Find(strValue, ActiveCell, , , , xlNext, False, , False).Activate
    
End Function

Open in new window


I then want to call this function from controls on user forms. Easy enough so far... However, I always get a type mismatch error when it tries to execute the function. I've tried passing the range as a range datatype as well as a variant, but it doesn't seem to make any difference.
I also tried adding the find code to the sub, but still get a type mismatch error. So, I'm using it incorrectly. What is wrong with the code?
0
Comment
Question by:Karl_mark
  • 2
4 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35070717
Try something like this

Public Function fCheckValueExists(ws As Worksheet, rng As String, _
strValue As String) As String
    Dim aCell As Range
    
    Set aCell = ws.Range(rng).Find(strValue, ActiveCell, , , , xlNext, False, , False)
    
    If Not aCell Is Nothing Then fCheckValueExists = aCell.Address
End Function

Open in new window


Sid
0
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 450 total points
ID: 35070733
OR as a boolean.

Public Function fCheckValueExists(ws As Worksheet, rng As String, _
strValue As String) As Boolean
    Dim aCell As Range
    
    Set aCell = ws.Range(rng).Find(strValue, ActiveCell, , , , xlNext, False, , False)
    
    If Not aCell Is Nothing Then fCheckValueExists = True
End Function

Open in new window


Sid
0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 50 total points
ID: 35070829
You need to remove the ActiveCell part from the Find call as well, unless you can guarantee that the activecell will be part of the range you specify (which looks unlikely)
0
 

Author Closing Comment

by:Karl_mark
ID: 35071309
Perfect Sid. Works a treat.
Good point by Rorya as well!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now