Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Passing a range to a UDF in VBA that references a different worksheet

Hi, I have a UDF as follows:

Function UniqueRows(rng As Range) As Integer
Dim iRow As Long
Dim iRow2 As Long
Dim iCol As Integer
Dim iCount As Integer
Dim colStrings As New Collection
Dim strKey As String

For iRow = 1 To rng.Rows.Count
strKey = ""
For iCol = 1 To rng.Columns.Count
strKey = strKey & "~" & rng.Cells(iRow, iCol).Text
Next iCol
On Error Resume Next
colStrings.Add strKey, strKey
On Error GoTo 0
Next iRow
UniqueRows = colStrings.Count
End Function

It produces the expected result when used on a worksheet even if the
range referenced is on a different one. However, when trying to use it
in a subroutine in VBA, I don't know how to pass a range to the UDF if
the range is not on the active worksheet.

I'm using a statement such as :
iUniqueRows =
UniqueRows(Worksheets("Sheet2").Range(Cells(iTLRow ,iTLCol),Cells(iBRRow,iBRCol)))

I get an error meesage about an Application error.

Any advice appreciated.

Regards, John.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Passing a range to a UDF in VBA that references a different worksh

As a guess one or more of your variables used in Cells is invalid. The code
itself is fine... What are the values of iTLRow,iTLCol, iBRRow,iBRCol when
the line is executed. I am guessing that at least 1 of them is 0...
--
HTH...

Jim Thomlinson


"jrisch" wrote:

Hi, I have a UDF as follows:

Function UniqueRows(rng As Range) As Integer
Dim iRow As Long
Dim iRow2 As Long
Dim iCol As Integer
Dim iCount As Integer
Dim colStrings As New Collection
Dim strKey As String

For iRow = 1 To rng.Rows.Count
strKey = ""
For iCol = 1 To rng.Columns.Count
strKey = strKey & "~" & rng.Cells(iRow, iCol).Text
Next iCol
On Error Resume Next
colStrings.Add strKey, strKey
On Error GoTo 0
Next iRow
UniqueRows = colStrings.Count
End Function

It produces the expected result when used on a worksheet even if the
range referenced is on a different one. However, when trying to use it
in a subroutine in VBA, I don't know how to pass a range to the UDF if
the range is not on the active worksheet.

I'm using a statement such as :
iUniqueRows =
UniqueRows(Worksheets("Sheet2").Range(Cells(iTLRow ,iTLCol),Cells(iBRRow,iBRCol)))

I get an error meesage about an Application error.

Any advice appreciated.

Regards, John.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Passing a range to a UDF in VBA that references a different worksh

With Worksheets("Sheet2")
iUniqueRows = UniqueRows(.Range( _
.Cells(iTLRow,iTLCol),.Cells(iBRRow,iBRCol)))
End with

Note the period (.) preceding Range, Cells and Cells. This qualifies all so
they are on the same sheet. If not, only range is referenced to
Worksheets("Sheet2"). the unqualified cells references refer to the
activesheet (if the code is in a general module) of to the sheet containing
the code (if the code is in a sheet module).

--
Regards,
Tom Ogilvy


"jrisch" wrote:

Hi, I have a UDF as follows:

Function UniqueRows(rng As Range) As Integer
Dim iRow As Long
Dim iRow2 As Long
Dim iCol As Integer
Dim iCount As Integer
Dim colStrings As New Collection
Dim strKey As String

For iRow = 1 To rng.Rows.Count
strKey = ""
For iCol = 1 To rng.Columns.Count
strKey = strKey & "~" & rng.Cells(iRow, iCol).Text
Next iCol
On Error Resume Next
colStrings.Add strKey, strKey
On Error GoTo 0
Next iRow
UniqueRows = colStrings.Count
End Function

It produces the expected result when used on a worksheet even if the
range referenced is on a different one. However, when trying to use it
in a subroutine in VBA, I don't know how to pass a range to the UDF if
the range is not on the active worksheet.

I'm using a statement such as :
iUniqueRows =
UniqueRows(Worksheets("Sheet2").Range(Cells(iTLRow ,iTLCol),Cells(iBRRow,iBRCol)))

I get an error meesage about an Application error.

Any advice appreciated.

Regards, John.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Passing a range to a UDF in VBA that references a different wo

I should know enough not to answer questions before my first cup of coffee.
Take a look at Tom's post... Mine is off the mark.
--
HTH...

Jim Thomlinson


"Jim Thomlinson" wrote:

As a guess one or more of your variables used in Cells is invalid. The code
itself is fine... What are the values of iTLRow,iTLCol, iBRRow,iBRCol when
the line is executed. I am guessing that at least 1 of them is 0...
--
HTH...

Jim Thomlinson


"jrisch" wrote:

Hi, I have a UDF as follows:

Function UniqueRows(rng As Range) As Integer
Dim iRow As Long
Dim iRow2 As Long
Dim iCol As Integer
Dim iCount As Integer
Dim colStrings As New Collection
Dim strKey As String

For iRow = 1 To rng.Rows.Count
strKey = ""
For iCol = 1 To rng.Columns.Count
strKey = strKey & "~" & rng.Cells(iRow, iCol).Text
Next iCol
On Error Resume Next
colStrings.Add strKey, strKey
On Error GoTo 0
Next iRow
UniqueRows = colStrings.Count
End Function

It produces the expected result when used on a worksheet even if the
range referenced is on a different one. However, when trying to use it
in a subroutine in VBA, I don't know how to pass a range to the UDF if
the range is not on the active worksheet.

I'm using a statement such as :
iUniqueRows =
UniqueRows(Worksheets("Sheet2").Range(Cells(iTLRow ,iTLCol),Cells(iBRRow,iBRCol)))

I get an error meesage about an Application error.

Any advice appreciated.

Regards, John.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Passing a range to a UDF in VBA that references a different wo

Thanks, both.

Tom, that explains a lot as I obviously have misunderstood how the
Range(Cells(,),Cells(,)) works for years :-(

Regards, John



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Passing a range to a UDF in VBA that references a different worksh

Many thanks, both. I now know I've misunderstood how
Range(Cells(,),Cells(,)) works, for years :-(

Regards,
John.

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
Trouble with passing a range into worksheet function in VBA [email protected] Excel Programming 3 January 27th 07 12:45 AM
Passing references Geoff Excel Programming 1 June 28th 06 08:50 PM
How to rename references from range names to cell references Abbas Excel Discussion (Misc queries) 1 May 24th 06 06:18 PM
Help with VBA code - passing references to sheets? Rayo K Excel Programming 7 April 20th 06 06:51 PM
passing a range from another worksheet Mike O Excel Programming 4 July 21st 05 05:09 PM


All times are GMT +1. The time now is 05:34 PM.

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"