Solved

excel VBA return range object from a function

Posted on 2001-06-06
3
4,255 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
ID: 6159088
0
 
LVL 2

Expert Comment

by:nfernand
ID: 6159408
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
ID: 6159959
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

685 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