Copy Macros from Personal Macro Workbook to new Workbook

Leonardo

Board Regular
Joined
Nov 8, 2011
Messages
57
I need to copy several macros from the Personal Macro workbook to a new workbook that I need to take to another computer. I need to be able to run the macros on that computer. I can only record macros and make minor changes in the VB editor. I do not know VB code. Can anyone help. Thank you?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
this should copy all the macro to a new workbook

Code:
Sub COPYALLMACROESTOANOTHERWORKBOOK()
    Dim vbcomp As object
    
    Dim mFolder As String: mFolder = "C:\Desktop\mymodules\"
    
    For Each vbcomp In ThisWorkbook.VBProject.VBComponents
        vbcomp.Export mFolder & vbcomp.Name & ".bas"
    Next
    
    Dim wb As Workbook: Set wb = Workbooks.Add
    Dim mFile As String: mFile = Dir(mFolder)
    
    While Not mFile = vbNullString
        If Right(mFile, 4) = ".bas" Then
            wb.VBProject.VBComponents.Import mFolder & mFile
        End If
    mFile = Dir
    Wend
End Sub
 
Upvote 0
this should copy all the macro to a new workbook

Code:
Sub COPYALLMACROESTOANOTHERWORKBOOK()
    Dim vbcomp As object
    
    Dim mFolder As String: mFolder = "C:\Desktop\mymodules\"
    
    For Each vbcomp In ThisWorkbook.VBProject.VBComponents
        vbcomp.Export mFolder & vbcomp.Name & ".bas"
    Next
    
    Dim wb As Workbook: Set wb = Workbooks.Add
    Dim mFile As String: mFile = Dir(mFolder)
    
    While Not mFile = vbNullString
        If Right(mFile, 4) = ".bas" Then
            wb.VBProject.VBComponents.Import mFolder & mFile
        End If
    mFile = Dir
    Wend
End Sub

VBA Geek, Thank you for your reply. Basically I only know how to record macros, so I am not sure how to install this code so that I can use it. Also will this copy the macros to an existing workbook or does it have to be newly opened? Does it prompt me for a name or location? Thanks again.
 
Upvote 0
Keeping in my mind that the easiest and fast way would be to just save your macro workbook with a different name and then send it to your other location.

You can use the above by going in the VBE Editor of your macro workbook, adding a new module and pasting that code in. It creates a new workbook and adds the modules that you've in your macro workbook to the new workbook.

You just need to update C:\Desktop\mymodules\ to location in your drive that will host the modules as they are copied from the macro workbook to the new workbook.
 
Upvote 0
Keeping in my mind that the easiest and fast way would be to just save your macro workbook with a different name and then send it to your other location.

You can use the above by going in the VBE Editor of your macro workbook, adding a new module and pasting that code in. It creates a new workbook and adds the modules that you've in your macro workbook to the new workbook.

You just need to update C:\Desktop\mymodules\ to location in your drive that will host the modules as they are copied from the macro workbook to the new workbook.

Thank you, I will give it a try.
 
Upvote 0
You can also just drag modules from one VBA project to another in the Project Explorer window. They always copy, not move.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,854
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top