Writing code into a Module via VBA & VBE

VenturSum

Board Regular
Joined
May 23, 2010
Messages
137
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:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,887
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
137
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,522
Messages
5,837,841
Members
430,518
Latest member
smithk901

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top