Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 61
Default first and last row of range

Hi,

Is there an easy way of capturing the first and last row of a range?

I can do it in a complicated way (assigning
ActiveWindow.RangeSelection.Address to a string variable and then parsing the
string variable to select the numbers within it which give me the first and
last row). But I suspect there exists a much simpler way to do it.

--
eugene
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default first and last row of range

Is the range a single area?

dim myRng as range
set myrng = activesheet.range("A9:g77") 'or something

with myrng
msgbox .row & vblf & .rows(.rows.count).row & vblf & .rows.count
end with

eugene wrote:

Hi,

Is there an easy way of capturing the first and last row of a range?

I can do it in a complicated way (assigning
ActiveWindow.RangeSelection.Address to a string variable and then parsing the
string variable to select the numbers within it which give me the first and
last row). But I suspect there exists a much simpler way to do it.

--
eugene


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 61
Default first and last row of range

I think I should clarify.

I have selected a range of cells (highlighted). I would like have a macro
determine the first and last row of the selection without the need for me to
specify anything at all about the range. The only information it has is the
fact that cells are selected.




--
eugene


"eugene" wrote:

Hi,

Is there an easy way of capturing the first and last row of a range?

I can do it in a complicated way (assigning
ActiveWindow.RangeSelection.Address to a string variable and then parsing the
string variable to select the numbers within it which give me the first and
last row). But I suspect there exists a much simpler way to do it.

--
eugene

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default first and last row of range

Try something like

Sub AAA()
Dim LastRow As Long
Dim LastCol As Long
Dim N As Long
N = Selection.Areas.Count
' OR
N = 1
LastRow = Selection.Areas(N).Cells(Selection.Areas(N).Rows.C ount, 1).Row
LastCol = Selection.Areas(N).Cells(1,
Selection.Areas(N).Columns.Count).Column
Debug.Print LastRow, LastCol

End Sub

The result will differ if you have multiple areas selected.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)

"eugene" wrote in message
...
I think I should clarify.

I have selected a range of cells (highlighted). I would like have a macro
determine the first and last row of the selection without the need for me
to
specify anything at all about the range. The only information it has is
the
fact that cells are selected.




--
eugene


"eugene" wrote:

Hi,

Is there an easy way of capturing the first and last row of a range?

I can do it in a complicated way (assigning
ActiveWindow.RangeSelection.Address to a string variable and then parsing
the
string variable to select the numbers within it which give me the first
and
last row). But I suspect there exists a much simpler way to do it.

--
eugene



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 61
Default first and last row of range

Dave'

Thanks. I set myrng = Selection (per the clarification I posted a few
minutes ago). Your code works perfectly for that as well.
--
eugene


"Dave Peterson" wrote:

Is the range a single area?

dim myRng as range
set myrng = activesheet.range("A9:g77") 'or something

with myrng
msgbox .row & vblf & .rows(.rows.count).row & vblf & .rows.count
end with

eugene wrote:

Hi,

Is there an easy way of capturing the first and last row of a range?

I can do it in a complicated way (assigning
ActiveWindow.RangeSelection.Address to a string variable and then parsing the
string variable to select the numbers within it which give me the first and
last row). But I suspect there exists a much simpler way to do it.

--
eugene


--

Dave Peterson



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 61
Default first and last row of range

Chip,

Thanks to you as weill (you guys are great with your quick responses).

I'll also try your code. It's always good to know more than one approach to
things.
--
eugene


"Chip Pearson" wrote:

Try something like

Sub AAA()
Dim LastRow As Long
Dim LastCol As Long
Dim N As Long
N = Selection.Areas.Count
' OR
N = 1
LastRow = Selection.Areas(N).Cells(Selection.Areas(N).Rows.C ount, 1).Row
LastCol = Selection.Areas(N).Cells(1,
Selection.Areas(N).Columns.Count).Column
Debug.Print LastRow, LastCol

End Sub

The result will differ if you have multiple areas selected.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)

"eugene" wrote in message
...
I think I should clarify.

I have selected a range of cells (highlighted). I would like have a macro
determine the first and last row of the selection without the need for me
to
specify anything at all about the range. The only information it has is
the
fact that cells are selected.




--
eugene


"eugene" wrote:

Hi,

Is there an easy way of capturing the first and last row of a range?

I can do it in a complicated way (assigning
ActiveWindow.RangeSelection.Address to a string variable and then parsing
the
string variable to select the numbers within it which give me the first
and
last row). But I suspect there exists a much simpler way to do it.

--
eugene




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
How do I enter formula sum(range+range)*0.15 sumif(range=3) tkw Excel Discussion (Misc queries) 2 October 1st 09 09:17 PM
Excel Addin:Setting the range to the Excel.Range object range prop Rp007 Excel Worksheet Functions 5 November 24th 06 04:30 PM
Range Question / error 1004: method Range of object Worksheet has failed Paul Excel Programming 3 April 7th 05 02:56 PM
Range.Find returns cell outside of range when range set to single cell Frank Jones Excel Programming 12 June 10th 04 04:22 AM
how to? set my range= my UDF argument (range vs. value in range) [advanced?] Keith R[_3_] Excel Programming 2 August 11th 03 05:55 PM


All times are GMT +1. The time now is 02:57 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"