Consulting

Results 1 to 7 of 7

Thread: Solved: How can I copy a UserForm (and Module)?

  1. #1
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location

    Solved: How can I copy a UserForm (and Module)?

    Is there some way to make a copy of a UserForm or a Module within the project?

    I've seen some clever code in here that manipuldates the VBE(?), so I'm hoping it's possible.

    Lot of times I have a UserForm all arranged (picture, buttons, frames, etc.) and I'd like to copy it to maintain the same style (template) for the rest of the UserForms.

    If it were just changing the label text or captions, etc. I could easily change them in VBA, but sometimes I need some more commandbuttons, or an EditBox and it's easier to just have another UserFOrm with the same basic layout.

    Same for a Module.

    Here's hoping


    Paul

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Right click and select Export. You can then Import again as a copy. You may need to rename the original to avoid name conflicts.
    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'

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    One way is to make a template of all of the controls on the controls toolbox.

    select all of the controls on the userform and drag them to the control toolbox. Then all you have to do is insert a new userform, make it the same size as the original and drag the controls from the toolbox all at one time.....check alignment on the upper left one and you got it.

    From the help file:
    Note Dragging a control (or a number of "grouped" controls) from the form back to the Toolbox creates a template of that control, which can be reused. This is a useful feature for implementing a standard interface for your applications.
    You can even right click on the tab of the toolbox and add sheets or tabs to hold your templates......

    I don't know how to automate it though.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Malcolms method will keep the code with the controls....I thought you were looking for an alternative to that.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Maclom --

    I try that, but I was wondering if there was a way to manipulate the VBE directly

    Other thing seems to be that you have to rename the original before you import the exported copy back in.

    Thanks

    Lucas -- I did want to keep the code with the copy of the controls, but I'll have to experiment with that template approach. That's a good tip

    Thanks

    Paul

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Const vbext_ct_StdModule As Long = 1
    Const vbext_ct_ClassModule As Long = 2
    Const vbext_ct_MSForm As Long = 3

    '----------------------------------------------------------------
    Sub CopyModule(ByVal modName As String)
    '----------------------------------------------------------------
    Dim VBMod As Object
    Dim SavedName As String
    Dim i As Long

    With ActiveWorkbook.VBProject

    Set VBMod = .VBComponents(modName)

    Select Case VBMod.Type

    Case vbext_ct_StdModule:

    VBMod.Export "C:\" & VBMod.Name & ".bas"
    .VBComponents.Import "C:\" & VBMod.Name & ".bas"

    Case vbext_ct_MSForm:

    SavedName = "C:\" & VBMod.Name & ".frm"
    VBMod.Export SavedName
    VBMod.Name = "OldForm"
    .VBComponents.Import SavedName
    Case vbext_ct_ClassModule:
    End Select
    End With
    End Sub

    Sub TestIt()

    CopyModule "Userform1"
    CopyModule "Module1"
    End Sub
    [/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

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Thanks everyone -- all good ideas

    I learned enougth here to do some better Google-ing and found some good information on Chip's page if any else is interested

    http://www.cpearson.com/excel/vbe.aspx

    Paul

Posting Permissions

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