+ Reply to Thread
Results 1 to 3 of 3

Can you create a macro that creates a macro and saves to a worksheet?

Hybrid View

  1. #1
    Registered User
    Join Date
    06-21-2012
    Location
    Evansville, IN
    MS-Off Ver
    Excel 2007
    Posts
    1

    Can you create a macro that creates a macro and saves to a worksheet?

    What I'm doing now:

    Opening a file that's saved as an .xml in Excel, and saving it as .xls

    What I need to add:

    When I open the file, as part of the macro, I need it to create another macro (that I already have the code for), and saves it WITHIN that excel sheet.

    So, I have a basic .xml, or .xls if you prefer, without any macro in it. During the macro that opens that file, it needs to write a macro to that excel file and embed it within. (The Macro will create a graph. The client doesn't want to simply create the graph-he wants the macro in there to give him the option of creating the graph or not).

    Is this even possible? If so...anyone have any pointers of where to even begin? This request has me completely baffled...

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Can you create a macro that creates a macro and saves to a worksheet?

    Hello rob.parson,

    Welcome to the Forum!

    Yes, this is possible using VBA. You would need to post the code for the main macro and the code for creating the chart.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Can you create a macro that creates a macro and saves to a worksheet?

    You could do something like this
    Sub test()
        Dim newBook As Workbook
        Dim newModule As Object
        
        Set newBook = Workbooks.Add
        
        Set newModule = newBook.VBProject.VBComponents.Add(1).CodeModule
        
        newModule.AddFromString myRoutineString
        
    End Sub
    
    Function myRoutineString() As String
        myRoutineString = "Sub test()"
        myRoutineString = myRoutineString & vbCr & "    MsgBox ""hello"""
        myRoutineString = myRoutineString & vbCr & "End Sub"
    End Function
    Alternalty, you could get the argument for AddFromString by reading an existing routine using .CodeModule.Lines
    Last edited by mikerickson; 06-21-2012 at 07:52 PM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

+ 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