Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

selected rows 1

Status
Not open for further replies.

Johnny42

Technical User
Jul 13, 2004
127
CA
I would like to loop all selected rows on an active sheet.
the rows can be continuous or a ctrl select....can anyone suggest ?
 
Hi,

Deja vous???
Code:
dim r as range
for each r in selection.rows
...
next


Skip,

[glasses] [red]Be advised:[/red]When Viscounts were guillotined just as they were disclosing where their jewels were hidden, it shows to go that you should...
Never hatchet your Counts before they chicken! [tongue]
 
Thanks Skip,

how can I just count the rows....if the user ctrl selected 2 cells on the same row ?
 

Code:
    Dim a As Range, areaCount, i
    areaCount = Selection.Areas.Count
    If areaCount <= 1 Then
        MsgBox "The selection contains " & _
            Selection.Rows.Count & " rows."
    Else
        i = 1
        For Each a In Selection.Areas
            MsgBox "Area " & i & " of the selection contains " & _
                a.Rows.Count & " rows."
            i = i + 1
        Next a
    End If

Skip,

[glasses] [red]Be advised:[/red]When Viscounts were guillotined just as they were disclosing where their jewels were hidden, it shows to go that you should...
Never hatchet your Counts before they chicken! [tongue]
 
Sorry must be missing it...

If user selects :

A1,B2,A4,B4,C6 I need rows selected = 4
I dont see how I can get this from your above post...:)

 
I think Skip may be taking his post-prandial nap.

You did say "selected rows" and not "selected cells." That kind of led us down the garden path.

Try something like this:
Code:
Sub test()
  MsgBox CountSelectedRows(Selection)
End Sub

Function CountSelectedRows(ARange As Range) As Integer
Dim c As Range
Dim a(100) As Boolean
Dim i As Integer
  For Each c In ARange
    a(c.Row) = True
  Next c
  For i = LBound(a) To UBound(a)
    If a(i) Then
      CountSelectedRows = CountSelectedRows + 1
    End If
  Next i
End Function
If you have more than 100 rows that may have selections, you will need to modify the array size accordingly.

 

Hey JOHHNIE,

Do I have to hold your hand and wipe your nose???

Connect the dots, son!
Code:
    Dim a As Range, areaCount, i
    areaCount = Selection.Areas.Count
    If areaCount <= 1 Then
        MsgBox "The selection contains " & _
            Selection.Rows.Count & " rows."
    Else
        i = 0
        For Each a In Selection.Areas
            i = i + 1
 '           MsgBox "Area " & i & " of the selection contains " & _
                a.Rows.Count & " rows."
        Next a
MsgBox i
    End If

Skip,

[glasses] [red]Be advised:[/red]When Viscounts were guillotined just as they were disclosing where their jewels were hidden, it shows to go that you should...
Never hatchet your Counts before they chicken! [tongue]
 

Easy there, Skip old buddy.

Try your code after selecting A1,C1,E1,B3 and D3. Your code announces "5", but only 2 rows are involved.

I think in that example, Johnny42 would want to see "2" not "5."

 

Johnny42,

I am sorry for my un-called-for outburst.

Z, thanx for pointing out the error of my ways.

[blush]

Skip,

[glasses] [red]Be advised:[/red]When Viscounts were guillotined just as they were disclosing where their jewels were hidden, it shows to go that you should...
Never hatchet your Counts before they chicken! [tongue]
 

Happy to oblige, Skip. We all have our bad days now and then. Not all of us own up to them, however.

I feel privileged to call you my friend. [thumbsup2]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top