Solved

Constructing a Range to use in Find Method Excel VBA

Posted on 2011-03-08
4
327 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

733 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