Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Range parameter on different sheet not working?

Hello all,

I'm having trouble with a function that takes a range as a parameter.
I'm using the function on one sheet, but passing in a range that is on
another sheet called "Data", so something like Foo(Data!A1:A10). The
formula seems to ignore the sheet reference and use A1:A10 on the page
from which the function is called... How can I make sure that the range
that is iterated through is on the correct sheet, not just the sheet
that calls the function?

function Foo(InputRange As Range)
Dim cl As Range

For Each cl In InputRange
'do something
' PROBLEM IS HE even if I pass "Data!A1:A10", the cl range
is on the current
' sheet, not the Data sheet.
Next cl
End Function

I tried something like this, but it didn't work:

Dim DataSheet As Worksheet
DataSheet = ActiveWorkbook.Sheets("Data")
Set cl = DataSheet.Range(cl)

You can see what I'm after though.

Thanks for your help...

Steve

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Range parameter on different sheet not working?

Try to select the right sheet prior to processing the cells:

Function foo(InputRange As Range) As Variant
Dim s As String
s = InputRange.Worksheet.Name
Sheets(s).Select


--
Gary's Student


"Steve Newhouse" wrote:

Hello all,

I'm having trouble with a function that takes a range as a parameter.
I'm using the function on one sheet, but passing in a range that is on
another sheet called "Data", so something like Foo(Data!A1:A10). The
formula seems to ignore the sheet reference and use A1:A10 on the page
from which the function is called... How can I make sure that the range
that is iterated through is on the correct sheet, not just the sheet
that calls the function?

function Foo(InputRange As Range)
Dim cl As Range

For Each cl In InputRange
'do something
' PROBLEM IS HE even if I pass "Data!A1:A10", the cl range
is on the current
' sheet, not the Data sheet.
Next cl
End Function

I tried something like this, but it didn't work:

Dim DataSheet As Worksheet
DataSheet = ActiveWorkbook.Sheets("Data")
Set cl = DataSheet.Range(cl)

You can see what I'm after though.

Thanks for your help...

Steve


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default Range parameter on different sheet not working?

I'm not sure how you're passing InputRange to Foo: you say
you do "something like Foo(Data!A1:A10)", but that isn't
valid VBA, and Foo("Data!A1:A10") gives a Type Mismatch.

The following works for me

Sub Main
Dim myRange as Range
Set myRange = ActiveWorkbook.Worksheets("Data").Range("A1:A10")
Foo myRange ' parens not needed when calling a Sub
End Sub



Steve Newhouse wrote:
Hello all,

I'm having trouble with a function that takes a range as a parameter.
I'm using the function on one sheet, but passing in a range that is on
another sheet called "Data", so something like Foo(Data!A1:A10). The
formula seems to ignore the sheet reference and use A1:A10 on the page
from which the function is called... How can I make sure that the range
that is iterated through is on the correct sheet, not just the sheet
that calls the function?

function Foo(InputRange As Range)
Dim cl As Range

For Each cl In InputRange
'do something
' PROBLEM IS HE even if I pass "Data!A1:A10", the cl range
is on the current
' sheet, not the Data sheet.
Next cl
End Function

I tried something like this, but it didn't work:

Dim DataSheet As Worksheet
DataSheet = ActiveWorkbook.Sheets("Data")
Set cl = DataSheet.Range(cl)

You can see what I'm after though.

Thanks for your help...

Steve


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
Adding a sort to a working query asks for a parameter value BRob Excel Worksheet Functions 0 July 23rd 08 10:28 AM
How to count a range of number to a given parameter or criteria. Saul Excel Worksheet Functions 4 October 26th 07 11:32 PM
Format when sending a range as parameter to a custom function George Furnell Excel Programming 2 December 1st 05 05:22 PM
Defining new function with cell range parameter NormD Excel Programming 2 March 1st 05 03:21 PM
Problem with named range as VBA macro parameter Ken[_11_] Excel Programming 4 March 5th 04 02:16 PM


All times are GMT +1. The time now is 10:23 AM.

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

About Us

"It's about Microsoft Excel"