Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default Passing a Range to a Function Problem

Hi when I run the TestIt sub below I get an error "Object Required". What
am I doing wrong?

Function FindIt(Ranger As Range) As String
Dim C As Range
Set C = Ranger.Find("BALER", LookIn:=xlValues)
If Not C Is Nothing Then FindIt = C.Address
End Function

Sub TestIt()
FindIt (Range("a1:a4"))
End Sub

--
Regards,
Rocky McKinley



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Passing a Range to a Function Problem

Rocky,

The problem is that you are enclosing the argument in parentheses
when you call the FindIt function. When you do this, VBA
evaluates that argument, and since the Value property is the
default property of a Range, VBA attempts to pass the value of
the range, not the actual range itself. You should enclose
arguments in parentheses only when the function call is returning
a value, or you want to force a ByRef argument to ByVal. Get rid
of the parentheses in the function call and you should be all
set. E.g.,

FindIt Range("a1:a4")


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Rocky McKinley" wrote in message
...
Hi when I run the TestIt sub below I get an error "Object

Required". What
am I doing wrong?

Function FindIt(Ranger As Range) As String
Dim C As Range
Set C = Ranger.Find("BALER", LookIn:=xlValues)
If Not C Is Nothing Then FindIt = C.Address
End Function

Sub TestIt()
FindIt (Range("a1:a4"))
End Sub

--
Regards,
Rocky McKinley





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default Passing a Range to a Function Problem

Thanks Chip, I appreciate the help.

--
Regards,
Rocky McKinley


"Chip Pearson" wrote in message
...
Rocky,

The problem is that you are enclosing the argument in parentheses
when you call the FindIt function. When you do this, VBA
evaluates that argument, and since the Value property is the
default property of a Range, VBA attempts to pass the value of
the range, not the actual range itself. You should enclose
arguments in parentheses only when the function call is returning
a value, or you want to force a ByRef argument to ByVal. Get rid
of the parentheses in the function call and you should be all
set. E.g.,

FindIt Range("a1:a4")


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Rocky McKinley" wrote in message
...
Hi when I run the TestIt sub below I get an error "Object

Required". What
am I doing wrong?

Function FindIt(Ranger As Range) As String
Dim C As Range
Set C = Ranger.Find("BALER", LookIn:=xlValues)
If Not C Is Nothing Then FindIt = C.Address
End Function

Sub TestIt()
FindIt (Range("a1:a4"))
End Sub

--
Regards,
Rocky McKinley







  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default Passing a Range to a Function Problem

Hi Chip there is still a problem, FindIt should return "" or an address.

Function FindIt(Ranger As Range) As String
Dim C As Range
FindIt = ""
Set C = Ranger.Find("BALER", LookIn:=xlValues)
If Not C Is Nothing Then FindIt = C.Address
End Function

Sub TestIt()
X = FindIt Range("a1:a4")
MsgBox X
End Sub

--
Regards,
Rocky McKinley


"Chip Pearson" wrote in message
...
Rocky,

The problem is that you are enclosing the argument in parentheses
when you call the FindIt function. When you do this, VBA
evaluates that argument, and since the Value property is the
default property of a Range, VBA attempts to pass the value of
the range, not the actual range itself. You should enclose
arguments in parentheses only when the function call is returning
a value, or you want to force a ByRef argument to ByVal. Get rid
of the parentheses in the function call and you should be all
set. E.g.,

FindIt Range("a1:a4")


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Rocky McKinley" wrote in message
...
Hi when I run the TestIt sub below I get an error "Object

Required". What
am I doing wrong?

Function FindIt(Ranger As Range) As String
Dim C As Range
Set C = Ranger.Find("BALER", LookIn:=xlValues)
If Not C Is Nothing Then FindIt = C.Address
End Function

Sub TestIt()
FindIt (Range("a1:a4"))
End Sub

--
Regards,
Rocky McKinley







  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Passing a Range to a Function Problem

since you are using the returned value of the function, you should enclose
the argument in brackets. this worked fine for me:

Function FindIt(Ranger As Range) As String
Dim C As Range
FindIt = ""
Set C = Ranger.Find("BALER", LookIn:=xlValues)
If Not C Is Nothing Then FindIt = C.Address
End Function

Sub TestIt()
Dim x As String
x = FindIt(Range("a1:a4"))
MsgBox x
End Sub


--
Regards,
Tom Ogilvy


Rocky McKinley wrote in message
...
Hi Chip there is still a problem, FindIt should return "" or an address.

Function FindIt(Ranger As Range) As String
Dim C As Range
FindIt = ""
Set C = Ranger.Find("BALER", LookIn:=xlValues)
If Not C Is Nothing Then FindIt = C.Address
End Function

Sub TestIt()
X = FindIt Range("a1:a4")
MsgBox X
End Sub

--
Regards,
Rocky McKinley


"Chip Pearson" wrote in message
...
Rocky,

The problem is that you are enclosing the argument in parentheses
when you call the FindIt function. When you do this, VBA
evaluates that argument, and since the Value property is the
default property of a Range, VBA attempts to pass the value of
the range, not the actual range itself. You should enclose
arguments in parentheses only when the function call is returning
a value, or you want to force a ByRef argument to ByVal. Get rid
of the parentheses in the function call and you should be all
set. E.g.,

FindIt Range("a1:a4")


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Rocky McKinley" wrote in message
...
Hi when I run the TestIt sub below I get an error "Object

Required". What
am I doing wrong?

Function FindIt(Ranger As Range) As String
Dim C As Range
Set C = Ranger.Find("BALER", LookIn:=xlValues)
If Not C Is Nothing Then FindIt = C.Address
End Function

Sub TestIt()
FindIt (Range("a1:a4"))
End Sub

--
Regards,
Rocky McKinley









Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing a range name as an argument to the Index Function Michael Sharpe Excel Discussion (Misc queries) 3 September 5th 12 01:33 PM
Passing a range to a user defined function Gary Nelson Excel Discussion (Misc queries) 1 July 19th 07 04:22 PM
passing a range to a user defined function using a form davek Excel Programming 1 December 24th 03 07:40 AM
Passing an unkown range to a function Newbie3333 Excel Programming 3 December 5th 03 02:14 AM
Passing range as argument Jan Kronsell[_2_] Excel Programming 3 September 3rd 03 12:31 PM


All times are GMT +1. The time now is 08:12 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"