Determine if selection contains more than one row

neodjandre

Well-known Member
Joined
Nov 29, 2006
Messages
946
Office Version
  1. 2019
Platform
  1. Windows
Hello,

I am looking for an IF statement to determine whether a selection of cells contains more than one rows.

could anyone help please?

thanks
Andy
 

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

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
38,801
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
If you need to be concerned with non-contiguous selections, then you will need to loop through each Area in the selection and add the Rows.Count of each one.
 

neodjandre

Well-known Member
Joined
Nov 29, 2006
Messages
946
Office Version
  1. 2019
Platform
  1. Windows
Rory,

I also need to be concerned with non-contiguous selections. Could you please show me with a code, how to achieve that?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
38,801
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS

ADVERTISEMENT

You could use something like:
Code:
Function RowCount(Optional rngInput) As Long
   Dim rngArea As Range
   Dim lngCounter As Long
   If IsMissing(rngInput) Then Set rngInput = Selection
   For Each rngArea In rngInput.Areas
      lngCounter = lngCounter + rngArea.Rows.Count
   Next rngArea
   RowCount = lngCounter
End Function

but be aware you will get double counting if the areas overlap in terms of rows.
 
Last edited:

Fazza

MrExcel MVP
Joined
May 17, 2006
Messages
9,368
Hi,

Non-contiguous selections could be in the same single row. Say cells A1 & C1

Maybe something like this? Regards, Fazza

Code:
Sub test()
 
  Dim bMoreThanOneRow As Boolean
  Dim lngRowFirst As Long
  Dim rngArea As Range
  Dim rngRow As Range
 
  bMoreThanOneRow = False
  lngRowFirst = Selection.Cells(1).Row
  For Each rngArea In Selection
    With rngArea
      If .Rows.Count > 1 Or .Cells(.Cells.Count).Row <> lngRowFirst Then
        bMoreThanOneRow = True
        Exit For
      End If
    End With
  Next rngArea
 
  MsgBox "More than one row : " & bMoreThanOneRow
 
End Sub
 

neodjandre

Well-known Member
Joined
Nov 29, 2006
Messages
946
Office Version
  1. 2019
Platform
  1. Windows
yeap, this sounds great as well :) thanks a ton
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,200
Messages
5,835,948
Members
430,396
Latest member
dzifna

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
Top