Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert a sub into an excel spreadsheet with vbs

Status
Not open for further replies.

sumgirl

IS-IT--Management
Mar 19, 2003
55
US
Anyone know how I can add a sub or function to an existing Excel spreadsheet from vbs? I dont want to have to open all the spreadsheets in order to do it.

Thanks.
 
Hi this should work for you - just change the paths/names of the spreadsheets and macro contents as required


Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Open("C:\temp\Test1.xls")
Set xlmodule = objworkbook.VBProject.VBComponents.Add(1)
strCode = _
"sub MyMacro()" & vbCr & _
" msgbox ""Inside generated macro!!!"" " & vbCr & _
"end sub"
xlmodule.CodeModule.AddFromString strCode

objworkbook.saveas "c:\temp\test2.xls"
objworkbook.close
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top