Results 1 to 11 of 11
  1. #1
    Star Lounger
    Join Date
    Feb 2005
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to access index number of Rows or Columns (Excel 2002)

    Hi,
    Trying to write a quick Excel VBA macro. Perhaps I am being "dim" but I can't figure out how to access the index number of Rows or Columns. Basically I want to check if it's the first row of a selection without using hard coded reference to a specific row number. (i.e. because the selection instead of beginning on Row 1 begins in Row N). But I can't figure out how to test for that:
    i.e. "If SelectedRange.Rows(1) = True Then" etc

    Or to put it another way, how can I to store that value in a given integer, say, FirstRow, as in
    FirstRow = Selection.Rows(1)

    Thanks!
    -Alex

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: How to access index number of Rows or Columns (Excel 2002)

    Have you tried
    FirstRow = Selection.Rows(1).Row
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: How to access index number of Rows or Columns (Excel 2002)

    You can use an even shorter version of Andrew's code:
    <code>FirstRow = Selection.Row</code>
    HTH
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    Star Lounger
    Join Date
    Feb 2005
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to access index number of Rows or Columns

    Oops, perhaps I wasn't clear. I already know how to get the literal now number. What I want is the index value (i.e. the "nth row number" of selection).
    Perhaps a better question is how to do:
    NthRow = Selection.Rows.??Index??

    Of course I could just run an extra loop (for x = 1 to Rows.Count), but I wanted to know if there is a built in way to access the index number of a row (not the literal row number).

    Thanks,
    Alex

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: How to access index number of Rows or Columns

    I'm afraid you've lost me. Can you provide an example that illustrates more clearly what you want?

  6. #6
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to access index number of Rows or Columns

    Sorry, I do not understand what you are asking. Do you want the offset of a particular row in the selection relative to the first row of the selection? If so, which row? Could you give a better description of what you are asking for?
    Legare Coleman

  7. #7
    Star Lounger
    Join Date
    Feb 2005
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to access index number of Rows or Columns

    Good God! Nobody understands me today! It's really not a big deal. I was just trying to access some information that I thought should be already available in a handy way.
    Basically, I want to know if I'm on the first row of my selection or the 2nd or the third, etc. i.e. As I loop through my cells in my selected range, I want to do something to each cell if they are on the first row, something else if they're not.

    So, basically, this is what I did that works but seems inelegant:
    ----------
    Dim Cell As Object
    Dim FirstRow As Integer
    Dim NthRow As Integer

    For Each Cell In SelectedRange.Cells

    FirstRow = SelectedRange.Row
    NthRow = Cell.Row - FirstRow + 1

    If NthRow = 1 Then
    ' Do something to cell
    Else
    ' Do something else
    End If
    Next Cell
    -------------
    So basically, I just wanted to do away with those 2 extra variables FirstRow and NthRow. I am sure they are many ways to do this, I just wanted to access that row index information of the cell, the "easy way".

    Again, not a big deal, just trying to learn.

    Sorry for the lack of clarity.
    Cheers,
    -Alex

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: How to access index number of Rows or Columns

    You could do something like this:

    Dim r As Long
    Dim c As Long
    For r = 1 To Selection.Rows.Count
    For c = 1 To Selection.Columns.Count
    If r = 1 Then
    ' Do something to Selection.Cells(r, c)
    Else
    ' Do something else to Selection.Cells(r, c)
    End If
    Next c
    Next r

    Selection can be replaced by a variable of type Range.

  9. #9
    Star Lounger
    Join Date
    Feb 2005
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to access index number of Rows or Columns

    Hi Hans,

    Ah, ok. I suppose that's the standard way to loop through a range in Vba-Excel? I figured I should try to use a For Each construct. Fair enough, next time I'll build the code that way. Thanks for the feedback.
    -Alex

  10. #10
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to access index number of Rows or Columns

    If you want to use For Each, then you can do something like:

    <code>
    Dim oCell As Range
    Dim lFirstRow As Long, lCRow As Long
    lFirstRow = Selection.Row
    For Each oCell In Selection
    If oCell.Row = lFirstRow Then
    MsgBox "First row."
    Else
    MsgBox "Not first row."
    End If
    Next oCell
    </code>
    Legare Coleman

  11. #11
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: How to access index number of Rows or Columns

    Looping is the standard way to select and operate on any group of cells, rows, columns, etc, but as an FYI, there's also the Offset method in Excel, for example:

    mySixthRowInSelection = Selection.Offset(6, 0).Row

    will return the absolute sheet row, that is if the selection starts on row 17, mySixthRowInSelection will return 23. Another toy to play with. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>
    -John ... I float in liquid gardens
    UTC -7ąDS

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •