Hi,
I have come up with the following code that attempts to imitate the auto-numbering function in MS Word for Excel. What the code does is to check for "Alt+Enter" entries used within a cell and will add a sequential number in front of it. I have attached a workbook that shows how it works. In addition, I have posted the code here too.
[vba]
Sub Numbering()
Dim i As Long, NewData As String, Chr10Pos As Long
i = 1
NumStyle (i)
For Each c In Selection
c.Offset(0, 1).Value = NumStyle(i) & c.Value
NewData = c.Offset(0, 1).Value
i = i + 1
For Each str1 In Array(Chr(10))
Chr10Pos = InStr(1, NewData, str1)
If Chr10Pos = 0 Then
Else
Do Until Chr10Pos = 0
NewData = Left(NewData, Chr10Pos) & NumStyle(i) & _
Right(NewData, Len(NewData) - Chr10Pos)
i = i + 1
Chr10Pos = InStr(Chr10Pos + 1, NewData, str1)
Loop
End If
Chr10Pos = 0
Next str1
i = 1
c.Offset(0, 1).Value = NewData
Next c
End Sub
Function NumStyle(i As Long) As String
NumStyle = i & ") " 'Defines the numbering style
End Function
[/vba]
I do realise that my code has limitations. I have indicated the limitations in my workbook. I would welcome any suggestions to improve the code. Nonetheless, I believe it should be useful for some people.
Regards,
X10A