Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default function to return a "sub-range"

I have a range AP4:AQ63 that has data in the top few cells in the range. The
remaining cells are empty (I use delete to clear them).

I need a routine to read this range and return a smaller range that only has
non-blank cells in it. For example, if there are only 15 rows filled in in
the range, the function would return AP4:AQ19.

I have a loop that goes through the range, I just need some help
constructing the range to return. Here is what I have so far (thanks to
snippets I've gotten from this forum):

Function GetSubRange() As Range

Dim r As Range
Set r = Worksheets("Subs").Range("AP4")

Do While Not r = ""
Set r = r.Offset(1, 0)
Loop

GetSubRange = ???

End Function
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 421
Default function to return a "sub-range"


Hi Ferdude,

Perhaps, trry something like:

Set myRng = Range("AP4").CurrentRegion


---
Regards.
Norman


"fedude" wrote in message
...
I have a range AP4:AQ63 that has data in the top few cells in the range.
The
remaining cells are empty (I use delete to clear them).

I need a routine to read this range and return a smaller range that only
has
non-blank cells in it. For example, if there are only 15 rows filled in
in
the range, the function would return AP4:AQ19.

I have a loop that goes through the range, I just need some help
constructing the range to return. Here is what I have so far (thanks to
snippets I've gotten from this forum):

Function GetSubRange() As Range

Dim r As Range
Set r = Worksheets("Subs").Range("AP4")

Do While Not r = ""
Set r = r.Offset(1, 0)
Loop

GetSubRange = ???

End Function


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default function to return a "sub-range"

Another one:

Option Explicit
Function GetSubRange(BeginningRng As Range) As Range

Dim TestRng As Range

'think negative!
Set GetSubRange = Nothing

If IsEmpty(BeginningRng.Cells(1).Value) Then
Exit Function
End If

'only one area!
Set BeginningRng = BeginningRng.Areas(1)

Set TestRng = Intersect(BeginningRng.Cells(1).CurrentRegion, BeginningRng)

Set TestRng = TestRng.Resize(, BeginningRng.Columns.Count)

Set GetSubRange = TestRng

End Function
Sub testme()

Dim myRng As Range
Dim mySubRng As Range

Set myRng = Worksheets("Subs").Range("ap4:aq63")

Set mySubRng = GetSubRange(myRng)

If mySubRng Is Nothing Then
MsgBox "No subrange"
Else
MsgBox mySubRng.Address
End If

End Sub


fedude wrote:

I have a range AP4:AQ63 that has data in the top few cells in the range. The
remaining cells are empty (I use delete to clear them).

I need a routine to read this range and return a smaller range that only has
non-blank cells in it. For example, if there are only 15 rows filled in in
the range, the function would return AP4:AQ19.

I have a loop that goes through the range, I just need some help
constructing the range to return. Here is what I have so far (thanks to
snippets I've gotten from this forum):

Function GetSubRange() As Range

Dim r As Range
Set r = Worksheets("Subs").Range("AP4")

Do While Not r = ""
Set r = r.Offset(1, 0)
Loop

GetSubRange = ???

End Function


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default function to return a "sub-range"

Won't this return the entire sheet?

"Norman Jones" wrote:


Hi Ferdude,

Perhaps, trry something like:

Set myRng = Range("AP4").CurrentRegion


---
Regards.
Norman


"fedude" wrote in message
...
I have a range AP4:AQ63 that has data in the top few cells in the range.
The
remaining cells are empty (I use delete to clear them).

I need a routine to read this range and return a smaller range that only
has
non-blank cells in it. For example, if there are only 15 rows filled in
in
the range, the function would return AP4:AQ19.

I have a loop that goes through the range, I just need some help
constructing the range to return. Here is what I have so far (thanks to
snippets I've gotten from this forum):

Function GetSubRange() As Range

Dim r As Range
Set r = Worksheets("Subs").Range("AP4")

Do While Not r = ""
Set r = r.Offset(1, 0)
Loop

GetSubRange = ???

End Function




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 421
Default function to return a "sub-range"

Hi Fedude,

If the columns on either side of the range
of interest, i.e. columns AO and AR, are empty,

Range("AP4").CurrentRegion

will return the range you require.

If this is not appropriate to your situation,
you have other responses.



---
Regards.
Norman


"fedude" wrote in message
...
Won't this return the entire sheet?

"Norman Jones" wrote:


Hi Ferdude,

Perhaps, trry something like:

Set myRng = Range("AP4").CurrentRegion


---
Regards.
Norman


"fedude" wrote in message
...
I have a range AP4:AQ63 that has data in the top few cells in the range.
The
remaining cells are empty (I use delete to clear them).

I need a routine to read this range and return a smaller range that
only
has
non-blank cells in it. For example, if there are only 15 rows filled
in
in
the range, the function would return AP4:AQ19.

I have a loop that goes through the range, I just need some help
constructing the range to return. Here is what I have so far (thanks
to
snippets I've gotten from this forum):

Function GetSubRange() As Range

Dim r As Range
Set r = Worksheets("Subs").Range("AP4")

Do While Not r = ""
Set r = r.Offset(1, 0)
Loop

GetSubRange = ???

End Function



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default function to return a "sub-range"

Dave,

This solution was perfect. Thank you. Not sure I totally understand all
the methods you employed, but it works and I can figure it out by stepping
through it.

The only gotcha (as mentioned by Norman Jones) in a previous post is that
the adjoining columns must be empty or it does not work. Took me a while to
catch this because initially, the adjoining columns were not blank.
Important safety tip....

Thanks!

"Dave Peterson" wrote:

Another one:

Option Explicit
Function GetSubRange(BeginningRng As Range) As Range

Dim TestRng As Range

'think negative!
Set GetSubRange = Nothing

If IsEmpty(BeginningRng.Cells(1).Value) Then
Exit Function
End If

'only one area!
Set BeginningRng = BeginningRng.Areas(1)

Set TestRng = Intersect(BeginningRng.Cells(1).CurrentRegion, BeginningRng)

Set TestRng = TestRng.Resize(, BeginningRng.Columns.Count)

Set GetSubRange = TestRng

End Function
Sub testme()

Dim myRng As Range
Dim mySubRng As Range

Set myRng = Worksheets("Subs").Range("ap4:aq63")

Set mySubRng = GetSubRange(myRng)

If mySubRng Is Nothing Then
MsgBox "No subrange"
Else
MsgBox mySubRng.Address
End If

End Sub


fedude wrote:

I have a range AP4:AQ63 that has data in the top few cells in the range. The
remaining cells are empty (I use delete to clear them).

I need a routine to read this range and return a smaller range that only has
non-blank cells in it. For example, if there are only 15 rows filled in in
the range, the function would return AP4:AQ19.

I have a loop that goes through the range, I just need some help
constructing the range to return. Here is what I have so far (thanks to
snippets I've gotten from this forum):

Function GetSubRange() As Range

Dim r As Range
Set r = Worksheets("Subs").Range("AP4")

Do While Not r = ""
Set r = r.Offset(1, 0)
Loop

GetSubRange = ???

End Function


--

Dave Peterson

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
Find a textstring in a "Range" - and then return a value iftextstring is found. HH[_3_] Excel Programming 6 January 29th 08 05:05 PM
function to return day in the form "Monday", "Tuesday" etc given . MTro Excel Worksheet Functions 2 October 3rd 07 09:49 AM
Check if cells contain the word "Thailand", return "TRUE" ali Excel Worksheet Functions 7 September 14th 07 09:53 AM
"No RETURN() or HALT() function found on macro sheet." Will Excel Worksheet Functions 2 January 4th 07 10:10 PM
use variable in Workbooks("book1").Worksheets("sheet1").Range("a1" Luc[_3_] Excel Programming 2 September 28th 05 08:37 PM


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