Microsoft Excel - VBA - pass range as function parameter

Asked By Aldo Liaks on 02-Jun-09 05:19 AM

Hi guys,

How do I pass a range to a function as a parameter?

Let's say I have the function

Function MyFunction(myRange as Range)

 'Do something with myRange...

End Function

I tried as follows by it doesn't compile.

MyFunction( Sheets(DataSheet).Range("B5:C9") )

MyFunction( "DataSheet!$B$5:$C$9" )

Thanks in advance for any help.

Aldo.

Harry L replied to Aldo Liaks on 02-Jun-09 05:48 AM

try this one

dim objSheetRange as Range
objSheetRange = Sheets(DataSheet).Range("B5:C9")
MyFunction( objSheetRange )

Run-time Error 91

Aldo Liaks replied to Harry L on 02-Jun-09 06:00 AM

Hi Harry,

I tried that, but getting run-time error 91 on the line below:

  objSheetRange = Sheets(DataSheet).Range("B5:C9")

Harry L replied to Aldo Liaks on 02-Jun-09 09:01 AM

Hi,

objSheetRange = Sheets(DataSheet).Range("B5:C9")

DataSheet is this a variable or sheet name?

if sheetname then it should be written like

objSheetRange = Sheets("DataSheet").Range("B5:C9")

and have included reference of excel in your application?


I did it exacly as you did,
Aldo Liaks replied to Harry L on 02-Jun-09 09:13 AM
but still doesn't work..
Possible Solution
Rolf Jaeger replied to Aldo Liaks on 03-Jun-09 02:10 PM

I tried the following VERY simple code and it works just fine. It seems that you need to CALL the function. When I simply go with CopyTest(Range("A5") I also get an error.

Have fun.

Sub Test()
    Call CopyTest(Range("A5"))
End Sub
Sub CopyTest(r As Range)
    r.Value = "Test2"
End Sub
Thanks!
Aldo Liaks replied to Rolf Jaeger on 04-Jun-09 12:40 AM
end of post
Kees replied to Rolf Jaeger on 30-Jul-10 07:27 AM
I gave a similar problem, I wish to hide a collection of rows based on a value in a range.  This can be achieved by a loop that hides them one by one. thios i want ot avoid because it is very slow when that range is linked to word as it triest update the ling in eacht step.  thus i want the sub to collect all rows, and them hide them all at once. The sub below works fine when I code the range I want to evaulate directy in the code.  I need to repeat this function for different ranges and thus would like call the function while simply passing the range instead of hard coding it. But this doesnt work :(  :(

This for example works fine


 call hiderows(hidevalue)
where the range is hardcoded.

This I cant get to work:
call hiderows(hidevalue,namedrange as Range)


Sub hiderows(RNG As range, hidevalue As Variant)
 
 
  Dim needToShow As range, needToHide As range
 
  Dim cell As range
  For Each cell In RNG

    If cell.Value = hidevalue And (cell.EntireRow.Hidden = False) Then
    If needToHide Is Nothing Then
      Set needToHide = cell
    Else
      Set needToHide = Union(needToHide, cell)
    End If
    End If

    If Not cell.Value = hidevalue And (cell.EntireRow.Hidden = True) Then
    If needToShow Is Nothing Then
      Set needToShow = cell
    Else
      Set needToShow = Union(needToShow, cell)
    End If
    End If
Next

    
If Not needToHide Is Nothing Then needToHide.EntireRow.Hidden = True
If Not needToShow Is Nothing Then needToShow.EntireRow.Hidden = False

End Sub
Kees replied to Kees on 30-Jul-10 07:31 AM
Solution:


It works when the range is passed as follows:


call hiderows(Range("A1:A20"),0)

now ill try named ranges
Henrik replied to Kees on 11-Aug-10 03:44 AM
Just wanted to say thanks.
I've been struggling with ranges in function calls for a very long time, and it just never occured to me that you actually have to send it as a range object. I always tried to send it as a string;
tmpVar = TempFunction("A1")

No wonder it never worked for me. :)
Zac Chacko replied to Rolf Jaeger on 04-Nov-11 06:10 AM
Thanks. I tried it and it worked. When passing a range to a function it worked when I added a "Call" before the function name!