Solved

excel VBA return range object from a function

Posted on 2001-06-06
3
4,105 Views
Last Modified: 2012-08-13
Dear all,

   I 've a function which will return a range object:

Dim temp as range
set temp = findit("abc")
....

end sub

Function Findit(Expr As String)
Dim col_head As Range
Range("A1").Activate
Set col_head = Cells.Find(What:=Expr, LookAt _
       :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
       MatchCase:=False)
Findit= col_head
End Function

However , the problem is that after the function is excuted, there is error when returning to the line set temp = findit("abc").do anyone know what's the problem?thanks!
0
Comment
Question by:hpchong7
3 Comments
 
LVL 13

Expert Comment

by:cri
Comment Utility
0
 
LVL 2

Expert Comment

by:nfernand
Comment Utility
Hi guys... another tips:

Specify the data type returned: Function Findit(Expr As String) as Range, this way VBA has not to deal with variants that are slower to handle.

If all of this is used as a function inside a cell, no error menssages may be generated. All you see the most is an #error# or #N/A# value in the cell.

As I know (maybe I'm wrong) the find function generates an error if data cannot be found. SO you have to prevent this by trappnig the error with "on error".

Bye.
0
 
LVL 6

Accepted Solution

by:
blakeh1 earned 20 total points
Comment Utility
I had problems running, it would actually error out on the
line
Findit= col_head
with an "Object Variable not set" error

I changed that line to
Set Findit = col_head
and I have no problem runnning it.
(Note: the variable will be "Nothing" if no match was found)

Sub testFindit()
    Dim temp As Range
    Set temp = Findit("ax")
End Sub
Function Findit(Expr As String) As Range
    Dim col_head As Range
    Range("A1").Activate
    Set col_head = Cells.Find(What:=Expr, LookAt _
          :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
          MatchCase:=False)
    Set Findit = col_head
End Function
 
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever had the experience that you had to follow 10 steps over and over again every time when you need to nicely forward an important email to your manager? Fear no more! With the help of the Quick Steps feature in Outlook 2010, your old chor…
My experience with Windows 10 over a one year period and suggestions for smooth operation
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

772 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

15 Experts available now in Live!

Get 1:1 Help Now