use one macro in different excel files

前端 未结 5 1662
我寻月下人不归
我寻月下人不归 2021-02-03 15:23

I wrote a macro, which I would like to use in different excel files, which have almost the same table structure but different data.

So is it possible anyhow just to \"in

5条回答
  •  花落未央
    2021-02-03 15:55

    The simplest, best way to do this is to create a "commander" XLSM which contains the macro but none of the data. All it does is open the specified Excel files and run the macro on them. You can either type/paste in the name of a specific file(s), you can loop through an entire directory, etc. It is very easy to run a macro on a different workbook than the one which actually contains the macro. Something like this below... piece of cake.

    Sub MultipleFileMacro()
    
    Dim wb As Excel.Workbook
    Dim fso As Scripting.FileSystemObject
    Dim f As Scripting.File
    
       Set fso = New Scripting.FileSystemObject
       For Each f In fso.GetFolder("c:\samplefolder").Files
          Set wb = Excel.Workbooks.Open(f)
    
          MyMacro wb
    
          wb.Close
       Next f
    
    End Sub
    ------------
    Sub MyMacro(wb As Excel.Workbook)
    
       'do something here
    
    End Sub
    

提交回复
热议问题