Determine if selection contains more than one row

neodjandre

Well-known Member
Joined
Nov 29, 2006
Messages
926
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
 

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,067
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
926
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
36,067
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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,483
Messages
5,596,405
Members
414,064
Latest member
Duncthegreat

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