Writing code into a Module via VBA & VBE

VenturSum

Board Regular
Joined
May 23, 2010
Messages
134
Team,

When I add a new worksheet, I must also copy to that sheet a command button. Next I need to add some code to the sheet's VBE object. And need some help here.

I've added the code to a string variable

Code:
sString = "Private Sub cboInsertText_Click()" & vbcrlf & _
          "Call InsertText" & vbcrlf & _
          "Sub End"
I've already changed the sheet's name. But I don't know what the sheet's "codename" is. So I need to address the VBE Object by the sheets' tab name.

Any Ideas??

John,
In Annapolis.
 
Last edited:

Some videos you may like

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,791
Hi John

Try this:

Code:
Sub AddWs_Code()
Dim wsh As Worksheet
Dim CodeM As CodeModule
Dim sString As String
 
sString = "Private Sub cboInsertText_Click()" & vbCrLf & _
          "Call InsertText" & vbCrLf & _
          "End Sub"
 
Set wsh = ActiveWorkbook.Worksheets.Add(After:=Sheets(Sheets.Count))
wsh.Name = "MyWorksheet"
 
Set CodeM = ActiveWorkbook.VBProject.VBComponents(wsh.CodeName).CodeModule
CodeM.InsertLines CodeM.CountOfLines + 1, sString
End Sub
Remark: I set the reference to "Microsoft Visual Basic for Applications Extensibility 5.3" to be able to use early binding.

If you don't set the reference, use

Code:
Dim CodeM As Object
 
Last edited:

VenturSum

Board Regular
Joined
May 23, 2010
Messages
134
PGC,

Thank you very much for this post.
The client uses Excel 2000,
so I'll have to see which extension library is on his machine.

Most Gratefully,

John,
In Annapolis
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,946
Messages
5,514,327
Members
408,997
Latest member
VJarvis

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top