Determine if selection contains more than one row

neodjandre

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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
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.
 
Upvote 0
Rory,

I also need to be concerned with non-contiguous selections. Could you please show me with a code, how to achieve that?
 
Upvote 0
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:
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,037
Members
448,543
Latest member
MartinLarkin

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