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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Excel Code Using Access

Status
Not open for further replies.

JesseH

MIS
Aug 9, 2001
63
US
I can write cells to an Excel worksheet. Carrying this a bit further, it stands to reason that I can write VBA code to an excel worksheet. The problem: in what location do I place it in the worksheet for excel to recognize as code (emulate Alt F11 in excel).

Thanks in Advance,

JesseH
 
You have to play with the VBProjects collection of the excel application VBE object.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Dear PNC, Do you happen to have an example?

Thanks
JesseH
 
You may have to change security settings in EXCEl to allow trusted access to visual basic project.

Sub AddcodetoExcel()

Dim objXl As Excel.Application
Dim objWkb As Workbook
Dim objSht As Worksheet


Set objXl = New Excel.Application
objXl.Visible = True
Set objWkb = objXl.Workbooks.Add

Dim xlModule As Object
Set xlModule = objWkb.VBProject.VBComponents.Add(1)

' Add a macro
Dim strCode As String
strCode = _
"sub MyMacro()" & vbCr & _
" msgbox ""LOOK AT THIS"" " & vbCr & _
"end sub"
xlModule.CodeModule.AddFromString strCode

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top