PC Review


Reply
Thread Tools Rate Thread

CurrentRegion - Usual Behavior?

 
 
Matthew Herbert
Guest
Posts: n/a
 
      25th Mar 2010
All,

I'm receiving some unexpected results with .CurrentRegion, but my
observations lend me to believe that using .CurrentRegion within a UDF that
is called from a worksheet (as opposed to being called internally within VBA)
does not work. (I thought that I might have read somewhere that this is the
case, but my memory is fuzzy on this topic). I'm trying to confirm if this
is the case, and if so, does this mean that one has to write a custom
function that does what .CurrentRegion does? I've provided an illustrative
example, along with my code, below.

Enter the following into the first worksheet (i.e. the left-most worksheet):
A1: Name; B1: Date; C1: Value
A2: Matt; B2: 3/24/2010; C2: 1
A3: Matt; B3: 3/24/2010; C3: 3
A4: <blank>; B4: 3/24/2010; C4: <blank>

If you run the "TestCurrentRegion" code below (on the spreadsheet data
listed above), then you should get the following results printed to the
Immediate Window (VBE: View | Immediate Window -or- Ctrl+g):
Rng.Adrs:$A$1
Cnt Rows: 4
Cnt Cols: 3
Rng.CrRg:$A$1:$C$4

However, if you enter the following formulas into the worksheet itself, you
get (or at least I got) a 1 for both results:
E1: =GetRegionCount(A1,1) --> Result = 1; Expected = 4
E2: =GetRegionCount(A1,2) --> Result = 1; Expected = 3

Again, does .CurrentRegion work only when called internally within VBA and
not when called from the worksheet?

Thanks,

Matthew Herbert

Sub TestCurrentRegion()
Dim Rng As Range
Set Rng = ThisWorkbook.Worksheets(1).Range("A1")
Debug.Print "Rng.Adrs:"; Rng.Address
Debug.Print "Cnt Rows:"; GetRegionCount(Rng, xlRows)
Debug.Print "Cnt Cols:"; GetRegionCount(Rng, xlColumns)
Debug.Print "Rng.CrRg:"; Rng.CurrentRegion.Address
Set Rng = Nothing
End Sub

Function GetRegionCount(rngReference As Range, uRowCol As XlRowCol) As Long
Dim Rng As Range
Set Rng = rngReference(1).CurrentRegion
With Rng
Select Case uRowCol
Case xlRows
GetRegionCount = .Rows.Count
Case xlColumns
GetRegionCount = .Columns.Count
End Select
End With
Set Rng = Nothing
End Function

 
Reply With Quote
 
 
 
 
Jim Cone
Guest
Posts: n/a
 
      25th Mar 2010

You can return a value (no formatting) to the cell containing the UDF call, but that's it.
You can't change/affect other cells. There may be some esoteric exceptions, but don't waste your
time.
--
Jim Cone
Portland, Oregon USA



"Matthew Herbert" <(E-Mail Removed)>
wrote in message news:F40ECB93-C1F3-464C-A199-(E-Mail Removed)...
All,

I'm receiving some unexpected results with .CurrentRegion, but my
observations lend me to believe that using .CurrentRegion within a UDF that
is called from a worksheet (as opposed to being called internally within VBA)
does not work. (I thought that I might have read somewhere that this is the
case, but my memory is fuzzy on this topic). I'm trying to confirm if this
is the case, and if so, does this mean that one has to write a custom
function that does what .CurrentRegion does? I've provided an illustrative
example, along with my code, below.

Enter the following into the first worksheet (i.e. the left-most worksheet):
A1: Name; B1: Date; C1: Value
A2: Matt; B2: 3/24/2010; C2: 1
A3: Matt; B3: 3/24/2010; C3: 3
A4: <blank>; B4: 3/24/2010; C4: <blank>

If you run the "TestCurrentRegion" code below (on the spreadsheet data
listed above), then you should get the following results printed to the
Immediate Window (VBE: View | Immediate Window -or- Ctrl+g):
Rng.Adrs:$A$1
Cnt Rows: 4
Cnt Cols: 3
Rng.CrRg:$A$1:$C$4

However, if you enter the following formulas into the worksheet itself, you
get (or at least I got) a 1 for both results:
E1: =GetRegionCount(A1,1) --> Result = 1; Expected = 4
E2: =GetRegionCount(A1,2) --> Result = 1; Expected = 3

Again, does .CurrentRegion work only when called internally within VBA and
not when called from the worksheet?

Thanks,

Matthew Herbert

Sub TestCurrentRegion()
Dim Rng As Range
Set Rng = ThisWorkbook.Worksheets(1).Range("A1")
Debug.Print "Rng.Adrs:"; Rng.Address
Debug.Print "Cnt Rows:"; GetRegionCount(Rng, xlRows)
Debug.Print "Cnt Cols:"; GetRegionCount(Rng, xlColumns)
Debug.Print "Rng.CrRg:"; Rng.CurrentRegion.Address
Set Rng = Nothing
End Sub

Function GetRegionCount(rngReference As Range, uRowCol As XlRowCol) As Long
Dim Rng As Range
Set Rng = rngReference(1).CurrentRegion
With Rng
Select Case uRowCol
Case xlRows
GetRegionCount = .Rows.Count
Case xlColumns
GetRegionCount = .Columns.Count
End Select
End With
Set Rng = Nothing
End Function

 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      25th Mar 2010
Some properties and methods cannot be used within a UDF like

..CurrentRegion
..CurrentArray
..Find
..SpecialCells

--
Jacob


"Matthew Herbert" wrote:

> All,
>
> I'm receiving some unexpected results with .CurrentRegion, but my
> observations lend me to believe that using .CurrentRegion within a UDF that
> is called from a worksheet (as opposed to being called internally within VBA)
> does not work. (I thought that I might have read somewhere that this is the
> case, but my memory is fuzzy on this topic). I'm trying to confirm if this
> is the case, and if so, does this mean that one has to write a custom
> function that does what .CurrentRegion does? I've provided an illustrative
> example, along with my code, below.
>
> Enter the following into the first worksheet (i.e. the left-most worksheet):
> A1: Name; B1: Date; C1: Value
> A2: Matt; B2: 3/24/2010; C2: 1
> A3: Matt; B3: 3/24/2010; C3: 3
> A4: <blank>; B4: 3/24/2010; C4: <blank>
>
> If you run the "TestCurrentRegion" code below (on the spreadsheet data
> listed above), then you should get the following results printed to the
> Immediate Window (VBE: View | Immediate Window -or- Ctrl+g):
> Rng.Adrs:$A$1
> Cnt Rows: 4
> Cnt Cols: 3
> Rng.CrRg:$A$1:$C$4
>
> However, if you enter the following formulas into the worksheet itself, you
> get (or at least I got) a 1 for both results:
> E1: =GetRegionCount(A1,1) --> Result = 1; Expected = 4
> E2: =GetRegionCount(A1,2) --> Result = 1; Expected = 3
>
> Again, does .CurrentRegion work only when called internally within VBA and
> not when called from the worksheet?
>
> Thanks,
>
> Matthew Herbert
>
> Sub TestCurrentRegion()
> Dim Rng As Range
> Set Rng = ThisWorkbook.Worksheets(1).Range("A1")
> Debug.Print "Rng.Adrs:"; Rng.Address
> Debug.Print "Cnt Rows:"; GetRegionCount(Rng, xlRows)
> Debug.Print "Cnt Cols:"; GetRegionCount(Rng, xlColumns)
> Debug.Print "Rng.CrRg:"; Rng.CurrentRegion.Address
> Set Rng = Nothing
> End Sub
>
> Function GetRegionCount(rngReference As Range, uRowCol As XlRowCol) As Long
> Dim Rng As Range
> Set Rng = rngReference(1).CurrentRegion
> With Rng
> Select Case uRowCol
> Case xlRows
> GetRegionCount = .Rows.Count
> Case xlColumns
> GetRegionCount = .Columns.Count
> End Select
> End With
> Set Rng = Nothing
> End Function
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Set CurrentRegion JLGWhiz Microsoft Excel Programming 6 7th Nov 2009 01:05 AM
currentregion =?Utf-8?B?ZmxvdzIz?= Microsoft Excel Misc 13 23rd Nov 2005 05:02 PM
CurrentRegion less one Row GregR Microsoft Excel Programming 8 7th Nov 2005 05:16 PM
UsedRange and CurrentRegion =?Utf-8?B?VG9tYXMgTS4=?= Microsoft Excel Programming 5 16th Sep 2004 03:23 PM
CurrentRegion Steph Microsoft Excel Programming 2 10th Aug 2004 07:00 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:53 AM.