Consulting

Results 1 to 5 of 5

Thread: Solved: VBA code to create / insert macro

  1. #1

    Solved: VBA code to create / insert macro

    HI,

    Is there any way by which an existing module in another excel book be copied / inserted in active workbook.

    Please suggest if any.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You could save the module in the other workbook, assumig it is open, then import that

    [vba]

    Workbooks("Book1").VBProject.VBComponents("Module1").Export "C:\myMod.txt"
    ThisWorkbook.VBProject.VBComponents.Import "C:\myMod.txt"
    Kill "c:\myMod.txt"
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I believe you cannot write a new file to the root directory in Vista, and in testing this code, this seems to be confirmed. The following adjustment works
    [vba]
    Workbooks("Book1").VBProject.VBComponents("Module1").Export "C:\AAA\myMod.txt"
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    As it used regulalry, best to setup a variable

    [vba]
    Const tempFile as string = "C:\temp\myMod.txt"

    Workbooks("Book1").VBProject.VBComponents("Module1").Export tempFile
    ThisWorkbook.VBProject.VBComponents.Import tempFile
    Kill tempFile
    [/vba]

    I definitely need to setup a Vista VM!
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Hi All,

    Thanks a lot.

    Bob,

    Thanks for the brilliant idea

    Regards
    Sai

Posting Permissions

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