Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Constructing a Range to use in Find Method Excel VBA

Posted on 2011-03-08
4
Medium Priority
?
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1800 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

636 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