Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Range definition problem

I'm trying to pass a Range to a Function but it doesn't recognize it
-- it gives an 'object required' msg.

If I set up a Watch on the Range, it says its type is Range/Range.

The Range itself looks ok on the screen.

Any ideas?
Peter.

Sub testMacro()
Dim dataRange As Range
Set dataRange = ActiveWorkbook.Sheets("Portfolio Performance"). _
Range(ActiveWorkbook.Sheets("Portfolio
Performance").Cells(10, 1), _
ActiveWorkbook.Sheets("Portfolio
Performance").Cells(20, 25))
dataRange.Interior.ColorIndex = 3 ' ok
TestThisVariant (dataRange) ' ok
TestThisRange (dataRange) ' object required
End Sub

Function TestThisRange(ByVal rRange As Range) As Boolean
TestThisRange = True
End Function

Function TestThisVariant(vRange)
End Function


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Range definition problem

Hi
try:

Dim ret_value as boolean
'...
ret_value=TestThisRange (dataRange)

--
Regards
Frank Kabel
Frankfurt, Germany

"Peter Chatterton" schrieb im Newsbeitrag
.. .
I'm trying to pass a Range to a Function but it doesn't recognize it
-- it gives an 'object required' msg.

If I set up a Watch on the Range, it says its type is Range/Range.

The Range itself looks ok on the screen.

Any ideas?
Peter.

Sub testMacro()
Dim dataRange As Range
Set dataRange = ActiveWorkbook.Sheets("Portfolio Performance"). _
Range(ActiveWorkbook.Sheets("Portfolio
Performance").Cells(10, 1), _
ActiveWorkbook.Sheets("Portfolio
Performance").Cells(20, 25))
dataRange.Interior.ColorIndex = 3 ' ok
TestThisVariant (dataRange) ' ok
TestThisRange (dataRange) ' object required
End Sub

Function TestThisRange(ByVal rRange As Range) As Boolean
TestThisRange = True
End Function

Function TestThisVariant(vRange)
End Function



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 983
Default Range definition problem

Two things.. First your object required issue arises because the function
wants to return a value, but you are not catching the value.

Secondly if I rememeber correctly VB/VBA does not like to pass ByVal into a
function and it defaults to ByRef no matter what.

Hope that helps...

"Peter Chatterton" wrote:

I'm trying to pass a Range to a Function but it doesn't recognize it
-- it gives an 'object required' msg.

If I set up a Watch on the Range, it says its type is Range/Range.

The Range itself looks ok on the screen.

Any ideas?
Peter.

Sub testMacro()
Dim dataRange As Range
Set dataRange = ActiveWorkbook.Sheets("Portfolio Performance"). _
Range(ActiveWorkbook.Sheets("Portfolio
Performance").Cells(10, 1), _
ActiveWorkbook.Sheets("Portfolio
Performance").Cells(20, 25))
dataRange.Interior.ColorIndex = 3 ' ok
TestThisVariant (dataRange) ' ok
TestThisRange (dataRange) ' object required
End Sub

Function TestThisRange(ByVal rRange As Range) As Boolean
TestThisRange = True
End Function

Function TestThisVariant(vRange)
End Function



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 983
Default Range definition problem

I just double checked and the ByVal thing is fine. I must be losing it...

"Jim Thomlinson" wrote:

Two things.. First your object required issue arises because the function
wants to return a value, but you are not catching the value.

Secondly if I rememeber correctly VB/VBA does not like to pass ByVal into a
function and it defaults to ByRef no matter what.

Hope that helps...

"Peter Chatterton" wrote:

I'm trying to pass a Range to a Function but it doesn't recognize it
-- it gives an 'object required' msg.

If I set up a Watch on the Range, it says its type is Range/Range.

The Range itself looks ok on the screen.

Any ideas?
Peter.

Sub testMacro()
Dim dataRange As Range
Set dataRange = ActiveWorkbook.Sheets("Portfolio Performance"). _
Range(ActiveWorkbook.Sheets("Portfolio
Performance").Cells(10, 1), _
ActiveWorkbook.Sheets("Portfolio
Performance").Cells(20, 25))
dataRange.Interior.ColorIndex = 3 ' ok
TestThisVariant (dataRange) ' ok
TestThisRange (dataRange) ' object required
End Sub

Function TestThisRange(ByVal rRange As Range) As Boolean
TestThisRange = True
End Function

Function TestThisVariant(vRange)
End Function



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Range definition problem

Function TestThisRange(ByVal rRange As Range) As Boolean
TestThisRange = True
End Function

I've tried coding this without the Boolean, with the same result.

I also stand by the way it's coded.

Thanks,
Peter.

"Peter Chatterton" wrote in message
.. .
I'm trying to pass a Range to a Function but it doesn't recognize it
-- it gives an 'object required' msg.

If I set up a Watch on the Range, it says its type is Range/Range.

The Range itself looks ok on the screen.

Any ideas?
Peter.

Sub testMacro()
Dim dataRange As Range
Set dataRange = ActiveWorkbook.Sheets("Portfolio Performance"). _
Range(ActiveWorkbook.Sheets("Portfolio
Performance").Cells(10, 1), _
ActiveWorkbook.Sheets("Portfolio
Performance").Cells(20, 25))
dataRange.Interior.ColorIndex = 3 ' ok
TestThisVariant (dataRange) ' ok
TestThisRange (dataRange) ' object required
End Sub

Function TestThisRange(ByVal rRange As Range) As Boolean
TestThisRange = True
End Function

Function TestThisVariant(vRange)
End Function






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Range definition problem

Peter,

Get rid of the parentheses in the call
TestThisRange (dataRange) ' object required
When you enclose arguments in parentheses to a procedure call
that does not return a value (or ignores the return value, as is
the case in your code), the argument within the parens is
evaluated and passed ByVal. Since the Value property is the
default property of a Range object, your code is equivalent to
TestThisRange dataRange.Value
And since TestThisRange expects a Range object, you get the
"Object Expected" error message.


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







"Peter Chatterton" wrote in message
.. .
I'm trying to pass a Range to a Function but it doesn't
recognize it
-- it gives an 'object required' msg.

If I set up a Watch on the Range, it says its type is
Range/Range.

The Range itself looks ok on the screen.

Any ideas?
Peter.

Sub testMacro()
Dim dataRange As Range
Set dataRange = ActiveWorkbook.Sheets("Portfolio
Performance"). _
Range(ActiveWorkbook.Sheets("Portfolio
Performance").Cells(10, 1), _
ActiveWorkbook.Sheets("Portfolio
Performance").Cells(20, 25))
dataRange.Interior.ColorIndex = 3 ' ok
TestThisVariant (dataRange) ' ok
TestThisRange (dataRange) ' object
required
End Sub

Function TestThisRange(ByVal rRange As Range) As Boolean
TestThisRange = True
End Function

Function TestThisVariant(vRange)
End Function




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Range definition problem

Sorry, your point 1 is right, I have to check the return value,
even tho it doesn't complain about it.
Peter

"Jim Thomlinson" wrote in message
...
Two things.. First your object required issue arises because the function
wants to return a value, but you are not catching the value.

Secondly if I rememeber correctly VB/VBA does not like to pass ByVal into
a
function and it defaults to ByRef no matter what.

Hope that helps...

"Peter Chatterton" wrote:

I'm trying to pass a Range to a Function but it doesn't recognize it
-- it gives an 'object required' msg.

If I set up a Watch on the Range, it says its type is Range/Range.

The Range itself looks ok on the screen.

Any ideas?
Peter.

Sub testMacro()
Dim dataRange As Range
Set dataRange = ActiveWorkbook.Sheets("Portfolio Performance"). _
Range(ActiveWorkbook.Sheets("Portfolio
Performance").Cells(10, 1), _
ActiveWorkbook.Sheets("Portfolio
Performance").Cells(20, 25))
dataRange.Interior.ColorIndex = 3 ' ok
TestThisVariant (dataRange) ' ok
TestThisRange (dataRange) ' object required
End Sub

Function TestThisRange(ByVal rRange As Range) As Boolean
TestThisRange = True
End Function

Function TestThisVariant(vRange)
End Function





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
Solver Problem Definition testspecmed Excel Worksheet Functions 0 October 5th 06 07:06 PM
VBA Range definition: Code needed David B Excel Discussion (Misc queries) 8 September 25th 06 06:40 PM
How come this range definition is invalid? keepITcool Excel Programming 1 June 21st 04 07:15 PM
variable range definition jmp Excel Programming 2 May 6th 04 11:06 PM
Using Cells( ) for Range definition [email protected] Excel Programming 5 September 2nd 03 08:04 PM


All times are GMT +1. The time now is 01:53 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"