Copy Macro between VBA files

Asked

Viewed 41 times

0

Hello, I have a file (I will call WB1) in Excel, with two macros. One of them (macroA) does a data processing and formatting. The other (macroB) creates a new file . xlsm (WB2) and copies some data from WB1.

However, WB2 also needs to have the macroA.

How can I make this "import" of a macro between the files in VBA? The import code would be inside macroB.

1 answer

1


Hi, there is a simpler way. Just create a new file based on your current. Follow the code illustrating this:

Option Explicit

Sub MacroA()
    Debug.Print "MacroA"
End Sub

Sub MacroB()
    Dim ArquivoNovo As String
    ArquivoNovo = Environ("TEMP") & "\" & Format(Now(), "YYYYMMDDHHMMSS") & "_" & ThisWorkbook.Name
    ThisWorkbook.SaveCopyAs ArquivoNovo
    
    Dim wbNovo As Workbook
    Set wbNovo = Workbooks.Open(ArquivoNovo)
    
    'Para usar comandos no arquivo original: ThisWorkbook
    MsgBox "Arquivo original= " & ThisWorkbook.Name
    
    'Para usar comandos no arquivo novo (copiado): wbNovo
    MsgBox "Arquivo original= " & wbNovo.Name
End Sub

Browser other questions tagged

You are not signed in. Login or sign up in order to post.