The short answer is: No, there is no line number "feature" in Excel VBAHi All reading this,
Can't figure out if there is a way to see line numbers in Modules/code?
Gracias
Sub t()
GoTo 5
Exit Sub
5:
MsgBox "5"
GoTo Five
Exit Sub
Five:
MsgBox "Five"
End Sub
Option Explicit
Sub AddLineNumbers()
Dim i As Long, j As Long, lineN As Long
Dim procName As String
Dim startOfProceedure As Long
Dim lengthOfProceedure As Long
With ThisWorkbook.VBProject.VBComponents("Userform1").CodeModule
For i = 1 To .CountOfLines
procName = .ProcOfLine(i, vbext_pk_Proc)
If procName <> vbNullString Then
startOfProceedure = .ProcStartLine(procName, vbext_pk_Proc)
If i = startOfProceedure Then
lengthOfProceedure = .ProcCountLines(procName, vbext_pk_Proc)
For j = 2 To lengthOfProceedure - 2
lineN = startOfProceedure + j
.ReplaceLine lineN, CStr(lineN) & ":" & RemoveOneLineNumber(.Lines(lineN, 1))
Next j
End If
End If
Next i
End With
End Sub
Sub RemoveLineNumbers()
Dim i As Long
With ThisWorkbook.VBProject.VBComponents("Userform1").CodeModule
For i = 1 To .CountOfLines
.ReplaceLine i, RemoveOneLineNumber(.Lines(i, 1))
Next i
End With
End Sub
Function RemoveOneLineNumber(aString)
RemoveOneLineNumber = aString
If aString Like "#:*" Or aString Like "##:*" Or aString Like "###:*" Then
RemoveOneLineNumber = Mid(aString, 1 + InStr(1, aString, ":", vbTextCompare))
End If
End Function
The short answer is: No, there is no line number "feature" in Excel VBA
Nalani, are you running an addon to see what you are seeing, or what menu did you click?
'in normal Module1
Option Explicit
Sub MakeUF()
With UserForm1
.Tag = "Choose a code module"
.Show
End With
End Sub
Sub AddLineNumbers(wbName As String, vbCompName As String)
Dim i As Long, j As Long, lineN As Long
Dim procName As String
Dim startOfProceedure As Long
Dim lengthOfProceedure As Long
Dim newLine As String
With Workbooks(wbName).VBProject.VBComponents(vbCompName).CodeModule
.CodePane.Window.Visible = False
For i = 1 To .CountOfLines
procName = .ProcOfLine(i, vbext_pk_Proc)
If procName <> vbNullString Then
startOfProceedure = .ProcStartLine(procName, vbext_pk_Proc)
lengthOfProceedure = .ProcCountLines(procName, vbext_pk_Proc)
If startOfProceedure + 1 < i And i < startOfProceedure + lengthOfProceedure - 1 Then
newLine = RemoveOneLineNumber(.Lines(i, 1))
If Not HasLabel(newLine) And Not (.Lines(i - 1, 1) Like "* _") Then
.ReplaceLine i, CStr(i) & ":" & newLine
End If
End If
End If
Next i
.CodePane.Window.Visible = True
End With
End Sub
Sub RemoveLineNumbers(wbName As String, vbCompName As String)
Dim i As Long
With Workbooks(wbName).VBProject.VBComponents(vbCompName).CodeModule
For i = 1 To .CountOfLines
.ReplaceLine i, RemoveOneLineNumber(.Lines(i, 1))
Next i
End With
End Sub
Function RemoveOneLineNumber(aString)
RemoveOneLineNumber = aString
If aString Like "#:*" Or aString Like "##:*" Or aString Like "###:*" Then
RemoveOneLineNumber = Mid(aString, 1 + InStr(1, aString, ":", vbTextCompare))
End If
End Function
Function HasLabel(ByVal aString As String) As Boolean
HasLabel = InStr(1, aString & ":", ":") < InStr(1, aString & " ", " ")
End Function
'in Userform1 code module
Option Explicit
Public WithEvents aListBox As MSForms.ListBox
Public WithEvents butOK As MSForms.CommandButton
Public WithEvents butCancel As MSForms.CommandButton
Public WithEvents butRemove As MSForms.CommandButton
Dim promptLabel As MSForms.Label
Private Sub aListBox_Click()
butOK.Enabled = True
butRemove.Enabled = True
End Sub
Private Sub butCancel_Click()
Me.Tag = vbNullString
Unload Me
End Sub
Private Sub butOK_Click()
With aListBox
If .ListIndex <> -1 Then
Call AddLineNumbers(.Value, .Text)
End If
End With
butOK.Enabled = False
butRemove.Enabled = True
aListBox.SetFocus
End Sub
Private Sub butRemove_Click()
With aListBox
If .ListIndex <> -1 Then
Call RemoveLineNumbers(.Value, .Text)
End If
End With
butRemove.Enabled = False
butOK.Enabled = True
aListBox.SetFocus
End Sub
Private Sub UserForm_Activate()
Dim oneWorkbook As Workbook
Dim oneComponent As VBComponent
Dim oneCodeModule As CodeModule
Dim sizeLabel As MSForms.Label
Dim fontName As String, fontSize As Long
fontName = "Arial": fontSize = 12
Set promptLabel = Me.Controls.Add("Forms.Label.1")
With promptLabel
With .Font
.Name = fontName: .Size = fontSize + 2
End With
.BorderStyle = fmBorderStyleNone
.Top = 5
.Left = 10
.Width = 400
.Caption = Me.Tag
.AutoSize = True
.WordWrap = True
.Width = 400
End With
Set aListBox = Me.Controls.Add("Forms.ListBox.1")
With aListBox
.Top = promptLabel.Top + promptLabel.Height + 10
.Left = promptLabel.Left
.Width = 400
.Height = 100
.ColumnCount = 2
.BoundColumn = 1: .TextColumn = 2
With .Font
.Name = fontName
.Size = fontSize
End With
End With
Set sizeLabel = Me.Controls.Add("Forms.Label.1")
With sizeLabel
With .Font
.Name = fontName
.Size = fontSize
End With
.AutoSize = True
.Visible = False
End With
For Each oneWorkbook In Application.Workbooks
If oneWorkbook.Windows(1).Visible Then
For Each oneComponent In oneWorkbook.VBProject.VBComponents
If Not ((oneWorkbook.Name = ThisWorkbook.Name And oneComponent.Name = "UserForm1") _
Or (oneWorkbook.Name = ThisWorkbook.Name And oneComponent.Name = "Module1")) Then
If oneComponent.Type <> vbext_ct_ClassModule Then
aListBox.AddItem oneWorkbook.Name
aListBox.List(aListBox.ListCount - 1, 1) = oneComponent.Name
sizeLabel.Caption = sizeLabel.Caption & vbCr & "X"
End If
End If
Next oneComponent
End If
Next oneWorkbook
aListBox.Height = sizeLabel.Height
Me.Controls.Remove sizeLabel.Name
Set butOK = Me.Controls.Add("Forms.CommandButton.1")
With butOK
With .Font
.Name = fontName
.Size = fontSize + 2
End With
.Default = True
.AutoSize = True
.Caption = "Add line labels"
.AutoSize = False
.Height = .Height - 4
.Top = aListBox.Top + aListBox.Height + 16
.Left = aListBox.Left + aListBox.Width - .Width
End With
Set butRemove = Me.Controls.Add("Forms.CommandButton.1")
With butRemove
With .Font
.Name = fontName
.Size = butOK.Font.Size
End With
.Caption = "Remove"
.Width = butOK.Width
.Height = butOK.Height
.Top = butOK.Top
.Left = butOK.Left - .Width - 20
End With
Set butCancel = Me.Controls.Add("Forms.CommandButton.1")
With butCancel
With .Font
.Name = fontName
.Size = butOK.Font.Size
End With
.Caption = "Close"
.Height = butOK.Height
.Width = butOK.Width
.Top = butOK.Top
.Left = butRemove.Left - .Width - 20
End With
With Me
.Width = 2 * aListBox.Left + aListBox.Width
.Height = butOK.Top + 2 * butOK.Height + 10
End With
butOK.Enabled = False
butRemove.Enabled = False
aListBox.SetFocus
End Sub