range of non-adjacent cells

Sahak

Well-known Member
Joined
Nov 10, 2006
Messages
1,012
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Hi all,

Please help me with macro code to return the row numbers of the first and last rows For a dynamic range of non-adjacent cells (for example for range E8:E21,E26:E33 should return 8 & 33)

Thank you in advance
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi

What would be the last row in the range E8:E21,E26:E33,H4:J20?

Would it be 20 (the last row in the last area) or 33 (the largest last row in any area)?
 
Upvote 0
Thank you very much for reply.

Last row in the range E8:E21,E26:E33,H4:J20 should be 33 (the largest last row in any area)
 
Upvote 0
hth

Dave

Code:
Sub LastRow()
    Dim rng1 As Range
    Dim rngArea As Range
    Dim lngRow As Long
    Set rng1 = Range("E8:E21,E26:E33,H4:J20")
    For Each rngArea In rng1.Areas
    lngRow = Application.Max(rngArea.Cells(1).Offset(rngArea.Rows.Count - 1, 1).Row, lngRow)
    Next rngArea
    MsgBox "Maximum row is " & lngRow
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,152
Members
452,891
Latest member
JUSTOUTOFMYREACH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top