Copy and paste your entire vba project that needs line numbers into activesheet cell A1.
Run writeLineNumbers.
Copy and paste column A & B back into your original project that needed line numbers. Voila, line numbers and (probably) no syntax errors.
It won't work if you have more than 3250 or so lines of code unless you dim linenum variables as long instead of int.
Please post any improvements, suggestions or problems. It could start over at 10 every time it encounters a new subroutine, or ignore end ifs and other lines that won't cause err msg, etc
Code
Sub writeLineNumbers()
Dim rw As Range
Dim lineNum As Integer
Dim previousRwNum As Integer
lineNum = 10
previousRwNum = 2
Cells(1, 1).EntireColumn.Insert
For Each rw In Rows("1:" & LastRow)
If rw.Row > 1 Then previousRwNum = rw.Row - 1
If Len(Cells(rw.Row, 2).Value) = _
Len(Cells(rw.Row, 2).Value) - Len(WorksheetFunction.Substitute(Cells(rw.Row, 2).Value, " ", "")) Then _
Cells(rw.Row, 2).ClearContents
If WorksheetFunction.CountBlank(Range(Cells(rw.Row, 2), Cells(rw.Row, 21))) <> 20 And _
WorksheetFunction.CountIf(Range(Cells(previousRwNum, 2), Cells(previousRwNum, 21)), "* _*") = 0 And _
WorksheetFunction.CountIf(Range(Cells(rw.Row, 2), Cells(rw.Row, 21)), "*Dim *") = 0 And _
WorksheetFunction.CountIf(Range(Cells(rw.Row, 2), Cells(rw.Row, 21)), "'*") = 0 And _
WorksheetFunction.CountIf(Range(Cells(rw.Row, 2), Cells(rw.Row, 21)), "*On Error*") = 0 And _
Left(Cells(rw.Row, 2).Value, 3) <> "Sub" And _
Left(Cells(rw.Row, 2).Value, 11) <> "Private Sub" And _
Left(Cells(rw.Row, 2).Value, 8) <> "Function" And _
Left(Cells(rw.Row, 2).Value, 12) <> "End Function" And _
Left(Cells(rw.Row, 2).Value, 7) <> "End Sub" Then
If lineNum = 10 Then
Cells(rw.Row, 1).Value = lineNum
lineNum = lineNum + 10
Else
Cells(rw.Row, 1).Value = "=add10(A:A)"
lineNum = lineNum + 10
End If
End If
Next rw
End Sub
Function LastRow() As Long
LastRow = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
End Function
Function add10(inRange As Range) As Integer
add10 = inRange.Find(What:="*", After:=Application.ThisCell, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Value + 10
End Function
Display More