View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Gary Keramidas[_4_] Gary Keramidas[_4_] is offline
external usenet poster
 
Posts: 226
Default can I use code to make code in another book

yes, but the way i normally use is to export the code module from the
existing workbook and import it into the new one.

as an example, this would export the shade_rows module, the the path i have
set in the fpath variable:

ThisWorkbook.VBProject.VBComponents("Mod_Shade_Row s").Export Filename:=fPath
& "Mod_Shade_Rows.bas"

then, after creating the new workbook, i import that module:
ActiveWorkbook.VBProject.VBComponents.Import Filename:=fPath &
"Mod_Shade_Rows.bas"

once in a while, i will create a code module, too.
this adds a workbook_open and workbook_beforeclose module, watch out for
wrapping by the forum/news reader. the long lines that wrap are all on one
line:

AddProcedureToModule()
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents("Thisworkbook")
Set CodeMod = VBComp.CodeModule

With CodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, "Private Sub Workbook_Open()"
LineNum = LineNum + 1
.InsertLines LineNum, "UserForm3.Show"
LineNum = LineNum + 1
.InsertLines LineNum, "Application.MacroOptions Macro:=" &
"""Show_UserForm3""" & ", Description:=""" & """" & ", ShortcutKey:=" &
"""F"""
LineNum = LineNum + 1
.InsertLines LineNum, "Application.MacroOptions Macro:=" &
"""Shade_Rows""" & ", Description:=""" & """" & ", ShortcutKey:=" & """P"""
LineNum = LineNum + 1
.InsertLines LineNum, "CreateMenubar3"
LineNum = LineNum + 1
.InsertLines LineNum, "End Sub"

LineNum = LineNum + 1
.InsertLines LineNum, "Private Sub Workbook_BeforeClose(Cancel
As Boolean)"
LineNum = LineNum + 1
.InsertLines LineNum, "RemoveMenubar3"
LineNum = LineNum + 1
.InsertLines LineNum, "Application.MacroOptions Macro:=" &
"""Show_UserForm3""" & ", Description:=""" & """" & ", ShortcutKey:=" &
""""""
LineNum = LineNum + 1
.InsertLines LineNum, "Application.MacroOptions Macro:=" &
"""Shade_Rows""" & ", Description:=""" & """" & ", ShortcutKey:=" & """"""
LineNum = LineNum + 1
.InsertLines LineNum, "End Sub"

End With


--


Gary Keramidas
Excel 2003


"Michelle" wrote in message
...
I want to write a macro that will make a new workbook and I want the new
workbook to have a macro in it.

Ideally, there will be a button on one of the sheets that will run it too.

is that possible?

Can I write a macro that will write a macro?

Thanks

M