Consulting

Results 1 to 8 of 8

Thread: Solved: Copy Module or UserForm into another file

  1. #1
    VBAX Tutor Philcjr's Avatar
    Joined
    Jul 2005
    Location
    Bedminster, NJ
    Posts
    208
    Location

    Solved: Copy Module or UserForm into another file

    All,

    This is a two question post:

    1) Is there a way to copy a Module (Formats) from from one file (Book1.xls or activeworkbook) to another file (Book2.xls)?

    2) Is there a way to copy a UserForm (Data) from from one file (Book1.xls or activeworkbook) to another file (Book2.xls)?

    Thanks for any insight on this,
    Phil

  2. #2
    VBAX Regular
    Joined
    Nov 2005
    Posts
    82
    Location
    Hi Phil

    The only way that I know how to do it is via dragging the module and/or userform from original worksheet in VBE to the new worksheet that you want to put it into. (Obviously with both of them open otherwise they aren't open in the VBE - I can't think of another way to do it from within the applications - short of inserting new module/userform and copying from original modules into the new ones on the new worksheet. You can always do it from the bas files etc, but that can be a bit painful.

    Hope it helps

    Cheers

    Amanda

  3. #3
    VBAX Tutor Philcjr's Avatar
    Joined
    Jul 2005
    Location
    Bedminster, NJ
    Posts
    208
    Location
    Amanda,
    Thanks for your insight, however, I was hoping there was a way to do this via some coding.

    Phil

  4. #4
    VBAX Regular
    Joined
    Nov 2005
    Posts
    82
    Location
    AhAH!

    I've got no doubt there is, but I'm only starting to take baby steps on that - so I'm afraid I can't help you - sorry

    Cheers

    Amanda

  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hey Phil,

    This code is from Chip Pearson's site and exports all modules - all - modules, forms, class modules ..

    [vba]Sub ExportAllVBA()
    Dim VBComp As VBIDE.VBComponent
    Dim Sfx As String

    For Each VBComp In ActiveWorkbook.VBProject.VBComponents
    Select Case VBComp.Type
    Case vbext_ct_ClassModule, vbext_ct_Document
    Sfx = ".cls"
    Case vbext_ct_MSForm
    Sfx = ".frm"
    Case vbext_ct_StdModule
    Sfx = ".bas"
    Case Else
    Sfx = ""
    End Select
    If Sfx <> "" Then
    VBComp.Export _
    Filename:=ActiveWorkbook.Path & "\" & VBComp.Name & Sfx
    End If
    Next VBComp
    End Sub[/vba]

    And Johnske has some code here to browse and import what you'd like ..

    http://vbaexpress.com/kb/getarticle.php?kb_id=397

    You could of course, combine these if you'd like to make it one process by defining both workbooks and using the Import/Export method.

    HTH

  6. #6
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    268
    Location
    I've got one sitting in the KB queue that allows importing / exporting to any open book. I wrote it for Mac originally because Mac VBE does not have the Import / Export commands, but it works with Windows too. If you'd like an 'advance copy', send me a PM.

    BTW, if you incorporate this kind of code it is quite likely that you will upset your virus software.

  7. #7
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Modify this to suit[vba]Option Explicit

    Public ThisBook As Workbook, OtherBook As Workbook

    Sub AmalgamateBooks()

    Set ThisBook = ActiveWorkbook

    'set reference to Microsoft Visual Basic
    'for Applications Extensibility 5.3
    On Error Resume Next
    ThisBook.VBProject.References.AddFromGuid _
    "{0002E157-0000-0000-C000-000000000046}", 5, 3

    Dim ImportCode$, ImportAllCode$, i&, N&

    ImportCode = MsgBox("Import the code modules from these workbooks?" & vbLf & _
    "" & vbLf & _
    "NOTE: Imported code may malfunction if there are" & vbLf & _
    "references to worksheets included in the code...", vbYesNo)
    If ImportCode = vbYes Then
    ImportAllCode = MsgBox("Include all 'ThisWorkbook' " & _
    "and Worksheet modules?" & vbLf & _
    "" & vbLf & _
    "NOTE: 'ThisWorkbook' and Sheet modules can only" & vbLf & _
    "be imported as ''Class'' modules...", vbYesNo)
    End If
    If ImportCode = vbYes And ImportAllCode = vbYes Then
    ImportCode = vbNo
    End If
    Application.ScreenUpdating = False
    With Application.FileSearch
    .LookIn = ActiveWorkbook.Path
    .FileName = "*.xls"
    If .Execute > 0 Then
    For i = 1 To .FoundFiles.Count
    If .FoundFiles(i) <> ThisWorkbook.FullName Then
    Application.Workbooks.Open(.FoundFiles(i)).Activate
    Set OtherBook = ActiveWorkbook
    With OtherBook
    For N = 1 To Sheets.Count
    Sheets(N).Activate
    Cells.Copy
    With ThisBook
    .Activate
    Worksheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Paste
    [A1].Select
    Application.CutCopyMode = False
    End With
    .Activate
    Next N
    If ImportCode = vbYes Then
    TransferModules
    ElseIf ImportAllCode = vbYes Then
    TransferAllModules
    End If
    End With
    With OtherBook
    .Close False
    End With
    End If
    Next i
    End If
    End With
    Sheet1.Activate
    End Sub

    Private Sub TransferModules()
    Dim Module$, Component As VBIDE.VBComponent
    With OtherBook
    Module = .Path & "\code.txt"
    For Each Component In .VBProject.VBComponents
    If Component.Type <> vbext_ct_Document Then
    Component.Export Module
    ThisBook.VBProject.VBComponents.Import Module
    End If
    Next Component
    End With
    End Sub

    Private Sub TransferAllModules()
    Dim Module$, Component As VBIDE.VBComponent
    With OtherBook
    Module = .Path & "\code.txt"
    For Each Component In .VBProject.VBComponents
    Component.Export Module
    ThisBook.VBProject.VBComponents.Import Module
    Next Component
    End With
    End Sub[/vba]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  8. #8
    VBAX Tutor Philcjr's Avatar
    Joined
    Jul 2005
    Location
    Bedminster, NJ
    Posts
    208
    Location
    Johnske,

    Very cool, this is a great start. THANK YOU!

    Phil

Posting Permissions

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