The following procedure adds code to the active module at line 3.
How to add code to the active module at the cursor position?
How to add code to the active module at the cursor position?
VBA Code:
Sub AddCode()
Dim wb As Workbook: Set wb = ActiveWorkbook
Dim xPro As VBIDE.VBProject
Dim xCom As VBIDE.VBComponent
Dim xMod As VBIDE.CodeModule
Set xPro = wb.VBProject
Set xCom = xPro.VBComponents(Application.VBE.ActiveCodePane.CodeModule.Name)
Set xMod = xCom.CodeModule
xMod.InsertLines 3, "Dim rng as range" & vbCrLf & _
"Set rng = Range([A2], Cells(Rows.Count,""A"").end(3))" & vbCrLf
End Sub