Results 1 to 9 of 9
  1. #1
    BakerMan
    Guest

    Numbering lines (2000)

    How would I write VBA code to select a range of cells say..A1 to A15 (I can do that part) and then number the cells in sequential order i.e.-1,2,3,..etc.

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

    Re: Numbering lines (2000)

    For example:

    Sub NumberCells()
    Dim i As Long
    For i = 1 To Selection.Rows.Count
    Selection.Cells(i, 1) = i
    Next i
    End Sub

    Select the cells first, then run the macro. I assumed that you wanted to number only cells in the first column of the selection.

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

    Re: Numbering lines (2000)

    In addition to what Hans replied, and depending on exactly what you are trying to do:

    <code>
    Range("A1").Value = 1
    Range("A1").AutoFill Range("A1:A15"), xlFillSeries
    </code>
    Legare Coleman

  4. #4
    BakerMan
    Guest

    Re: Numbering lines (2000)

    That's what I need thanks

  5. #5
    New Lounger
    Join Date
    Mar 2006
    Location
    Wesseling, Nordrhein-Westfalen, Germany
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Numbering lines - dynamic length

    Hello,

    lines are numbered from A1 to last used row in the sheet:

    Sub TestNo4()
    Range(Cells(1, "A"), Cells(ActiveSheet.Cells(Cells(Rows.Count, "A").End(xlUp).Row, 1).Row, "A")).Select
    Dim x As Long, y As Integer
    x = ActiveSheet.UsedRange.Rows.Count
    y = ActiveCell.Column
    Range("A1") = 1
    Range("A1").AutoFill Range(Cells(ActiveCell.Row, y), Cells(x, y)), xlFillSeries
    Range("A1").Select
    End Sub

    Regards,
    Beate

  6. #6
    BakerMan
    Guest

    Re: Numbering lines - dynamic length

    thanks all

  7. #7
    stanlafayette
    Guest

    Re: Numbering lines (2000)

    In POST 564330 on March 19th I asked the question :
    How would I write VBA code to select a range of cells say..A1 to A15 (I can do that part) and then number the cells in sequential order i.e.-1,2,3,..etc.
    and you gave me a solution which seems to work fine except that I end up with a number "1" in the bottom cell 1.e. 366,367,1.

    For i = 1 To Selection.Rows.Count
    Selection.Cells(i, 1) = i
    Next i

    What am I doing wrong??????? <img src=/S/frown.gif border=0 alt=frown width=15 height=15>

  8. #8
    stanlafayette
    Guest

    Re: Numbering lines (2000)

    I meant to make a new post ---sorry

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

    Re: Numbering lines (2000)

    I don't see how the code snippet you posted could do that. Is it possible that the 1 was already in the cell below the last cell you selected before you ran the macro? If not, could you post all of your code, preferably by uploading a workbook that we can use to test what is happening?
    Legare Coleman

Posting Permissions

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