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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return Number of Rows/Columns in a selection.

Status
Not open for further replies.

Aeneas

Programmer
Sep 18, 2003
25
CA
I want to loop through a selection, looping through the columns and then the rows.

"selection.rows" returns the array, which one can then tack on stuff like ".cells(1,1)" but how do I get the length of the array in rows and columns?

Thanks in advance as I know this should be pretty easy.
 
Hi,
Code:
With Selection
  NbrRows = .Rows.Count
  NbrCols = .Columns.Count
  FirstRow = .Row
  FirstCol = .Column
  LastRow = FirstRow + NbrRows - 1
  LastCol = FirstCol + NbrCols - 1
End With
But you do not necessarily need these values to loop thru a range...
Code:
Dim s As Range
For Each s in Selection
  With s
    If .Value <> "" Then
      'I have a value
      .Interior.Color = vbGreen
    Else
      .Interior.Color = vbRed
    End If
  End With
Next


Skip,

[glasses] [red]Be advised:[/red] When transmitting sheet music...
If it ain't baroque, don't fax it! [tongue]
 
Right, right. I've used count before, just couldn't remember it and it was a while ago. You da man, Skip. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top