Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

excel VBA return range object from a function

Posted on 2001-06-06
3
Medium Priority
?
4,415 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
[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
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 80 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

715 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