ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Add Procedure To Module Via VBA (https://www.excelbanter.com/excel-programming/329824-add-procedure-module-via-vba.html)

al

Add Procedure To Module Via VBA
 
I'd like to add a Procedure to a Worksheet Module via VBA.
The problem is that the module already contains a procedure,
so I need to learn how to add the procedure UNDER the existing procedure.
Attached is the procedure I'd like to add. Thanks in advance.


Sub addselectionchange()
Dim StartLine As Long
Dim VBCodeMod As CodeModule
Dim DEPT3 As String
Dim LINENUM As Integer

Set VBCodeMod =
ThisWorkbook.VBProject.VBComponents(Worksheets("Al l").CodeName).CodeModule

With VBCodeMod

StartLine = .CreateEventProc("SelectionChange", "Worksheet") + 1

.InsertLines StartLine, _
"lastAddress = Target.Address"


End With

End Sub

Bob Phillips[_6_]

Add Procedure To Module Via VBA
 
Al,

Have you tried this. It does exactly what you want?

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Al" wrote in message
...
I'd like to add a Procedure to a Worksheet Module via VBA.
The problem is that the module already contains a procedure,
so I need to learn how to add the procedure UNDER the existing procedure.
Attached is the procedure I'd like to add. Thanks in advance.


Sub addselectionchange()
Dim StartLine As Long
Dim VBCodeMod As CodeModule
Dim DEPT3 As String
Dim LINENUM As Integer

Set VBCodeMod =
ThisWorkbook.VBProject.VBComponents(Worksheets("Al l").CodeName).CodeModule

With VBCodeMod

StartLine = .CreateEventProc("SelectionChange", "Worksheet") + 1

.InsertLines StartLine, _
"lastAddress = Target.Address"


End With

End Sub




al

Add Procedure To Module Via VBA
 
Bob:

Thanks for responding.

The procedure itself runs fine if there ISNT another procedure in the module
I want to copy this procedure to.

The problem is that there is already another procedure within the module
that I need this procedure in. It attempts to overwrite that procedure.
Consequently Errors are caused and Excel eventually shuts down Completely.

"Bob Phillips" wrote:

Al,

Have you tried this. It does exactly what you want?

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Al" wrote in message
...
I'd like to add a Procedure to a Worksheet Module via VBA.
The problem is that the module already contains a procedure,
so I need to learn how to add the procedure UNDER the existing procedure.
Attached is the procedure I'd like to add. Thanks in advance.


Sub addselectionchange()
Dim StartLine As Long
Dim VBCodeMod As CodeModule
Dim DEPT3 As String
Dim LINENUM As Integer

Set VBCodeMod =
ThisWorkbook.VBProject.VBComponents(Worksheets("Al l").CodeName).CodeModule

With VBCodeMod

StartLine = .CreateEventProc("SelectionChange", "Worksheet") + 1

.InsertLines StartLine, _
"lastAddress = Target.Address"


End With

End Sub






All times are GMT +1. The time now is 01:38 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com