+ Reply to Thread
Results 1 to 12 of 12

Can macro create another macro?

Hybrid View

  1. #1
    Jef Gorbach
    Guest

    Re: Can macro create another macro?


    "VK" <"myname"@example.invalid> wrote in message
    news:3fm4vgF8gch8U1@news.dfncis.de...
    > > If it is an event macro, use something like
    > >
    > > Dim iStartAs Long
    > >
    > > With

    ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
    > > iStart = .CreateEventProc("BeforeSave", "Workbook") + 1
    > > .InsertLines iStart, _
    > > "Dim ans" & vbCrLf & _
    > > " ans = Msgbox( ""All OK"",vbYesNo)" & vbCrLf & _
    > > " If ans = vbNo Then Cancel = True"
    > > End With
    > >
    > > If it just another macro in Module1 say, then use
    > >
    > > Dim iNext As Long
    > >
    > > With ThisWorkbook.VBProject.VBComponents("Module1").CodeModule
    > > iNext = .CountOfLines + 1
    > > .InsertLines iNext, _
    > > "Sub myProc()" & vbNewLine & _
    > > " Msgbox ""Tesing new procedure"" " & vbNewLine & _
    > > "End Sub"
    > > End With
    > >

    >
    > Thanks! I'll try it out
    > Reg. VK


    Would like todo same with about 5 multi-page routines, so wonder if instead
    of embedding the new code in the copying macro (per above), if its possible
    to copy an existing macro between files, say from personal.xls to book1.xls
    ??

    ie:

    Sub test()
    Workbooks("Book1").VBProject.VBComponents.Import _
    (Workbooks("Personal").VBProject.vbcompoents("aging_report").codemodule)
    End Sub

    seems logical, but returns "runtime error 438: object does not support this
    property or method"





  2. #2
    Bob Phillips
    Guest

    Re: Can macro create another macro?

    You cannot import a module from another workbook, you must import from a
    file. So you export from Personal.xls to a file, and then import that file
    into another book.

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Jef Gorbach" <Jefgorbach@aol.com> wrote in message
    news:OCiqjbhYFHA.2796@TK2MSFTNGP09.phx.gbl...
    >
    > "VK" <"myname"@example.invalid> wrote in message
    > news:3fm4vgF8gch8U1@news.dfncis.de...
    > > > If it is an event macro, use something like
    > > >
    > > > Dim iStartAs Long
    > > >
    > > > With

    > ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
    > > > iStart = .CreateEventProc("BeforeSave", "Workbook") + 1
    > > > .InsertLines iStart, _
    > > > "Dim ans" & vbCrLf & _
    > > > " ans = Msgbox( ""All OK"",vbYesNo)" & vbCrLf & _
    > > > " If ans = vbNo Then Cancel = True"
    > > > End With
    > > >
    > > > If it just another macro in Module1 say, then use
    > > >
    > > > Dim iNext As Long
    > > >
    > > > With ThisWorkbook.VBProject.VBComponents("Module1").CodeModule
    > > > iNext = .CountOfLines + 1
    > > > .InsertLines iNext, _
    > > > "Sub myProc()" & vbNewLine & _
    > > > " Msgbox ""Tesing new procedure"" " & vbNewLine & _
    > > > "End Sub"
    > > > End With
    > > >

    > >
    > > Thanks! I'll try it out
    > > Reg. VK

    >
    > Would like todo same with about 5 multi-page routines, so wonder if

    instead
    > of embedding the new code in the copying macro (per above), if its

    possible
    > to copy an existing macro between files, say from personal.xls to

    book1.xls
    > ??
    >
    > ie:
    >
    > Sub test()
    > Workbooks("Book1").VBProject.VBComponents.Import _
    >

    (Workbooks("Personal").VBProject.vbcompoents("aging_report").codemodule)
    > End Sub
    >
    > seems logical, but returns "runtime error 438: object does not support

    this
    > property or method"
    >
    >
    >
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1