LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 149
Default CurrentRegion - Usual Behavior?

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

 
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
Not your usual password question! Andy Excel Discussion (Misc queries) 6 December 1st 09 03:31 PM
Help with an usual graph Sally Excel Discussion (Misc queries) 1 July 20th 06 02:15 PM
Page Break View - not the usual question Rose Excel Discussion (Misc queries) 0 March 2nd 05 03:25 PM
Textbox's and Dates the usual conflict Marcus Walker Excel Programming 1 October 19th 04 02:52 PM


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