vbaexcelautofillautonumber

VBA auto number cells that contain text


I am trying to find a solution for the following problem: column A contains names and I would like to keep those names there and use a code that will auto number them. Something like:

enter image description here

I have looked at quite a few examples on StackOverflow but what those codes do is mainly auto number some cells without necesarily keeping the text in the same cell: vba auto increment a number?
Could you please help me?

Here is the code I used but I only managed to insert numbers, not keep the text in the same cell.

Sub autonumber()


Dim i As Integer
Dim cell As Range, rng As Range

Set rng = Range("A1:A10")

i = 1

For Each cell In rng
    cell.Value = "" & i
    i = i + 1
Next cell

End Sub

Solution

  • Change

    cell.Value = "" & i
    

    to

    cell.Value = i & ". " & cell.Value
    

    Also if you just want to select and auto-number then you don't need rng at all: just use

    For Each cell In Selection