Run Macro Data Export based on name of workbook on sheet1

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,563
Office Version
  1. 2021
Platform
  1. Windows
I have workbook names in sheet1. I would like to run the macro "Data Export" in each of these workbooks instead of macro below. If A new file/s files are added I need to amend the macro below each time



There could be between 15 -20 workbooks on which to run the macro

Code:
Sub Export_TB() 's()

Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    Application.CutCopyMode = False

 Windows("M_BR1 ACCNTS(P).xlsm").Activate
    Application.Run "'M_BR1 ACCNTS(P).xlsm'!Data Export"
    ActiveWorkbook.Close
     Windows("M_BR1 ACCNTS(P).xlsm").Activate
     ActiveWorkbook.Close
    Windows("Open Pull Files.xlsm").Activate

 Windows("M_BR2 ACCNTS(P).xlsm").Activate
    Application.Run "'M_BR2 ACCNTS(P).xlsm'!Data Export"
    ActiveWorkbook.Close
     Windows("M_BR2 ACCNTS(P).xlsm").Activate
     ActiveWorkbook.Close
    Windows("Open Pull Files.xlsm").Activate
    
Windows("M_BR3 ACCNTS(P).xlsm").Activate
    Application.Run "'M_BR3 ACCNTS(P).xlsm'!Data Export"
    ActiveWorkbook.Close
     Windows("M_BR3 ACCNTS(P).xlsm").Activate
     ActiveWorkbook.Close
    Windows("Open Pull Files.xlsm").Activate
Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.CutCopyMode = False


End Sub


It would be appreciated if someone could assist me
 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
See if this works for you.
Code:
Sub Export_TB() 's()
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
        .EnableEvents = False
        .CutCopyMode = False
    End With
    Dim x As Long, lastrow As Long
    lastrow = Sheets("Workspace").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For x = 1 To lastrow
        Windows("M_BR" & x & "ACCNTS(P).xlsm").Activate
        Application.Run "'M_BR" & x & "ACCNTS(P).xlsm'!Data Export"
        ActiveWorkbook.Close
        Windows("M_BR" & x & "ACCNTS(P).xlsm").Activate
        ActiveWorkbook.Close
        Windows("Open Pull Files.xlsm").Activate
    Next x
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
        .EnableEvents = True
        .CutCopyMode = True
    End With
End Sub
 
Upvote 0
Thanks for the help

I get subscript out of range and the following code is highlighted

Code:
   Windows("M_BR" & x & "ACCNTS(P).xlsm").Activate


Please check & amend
 
Upvote 0
Try:
Code:
Sub Export_TB() 's()
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
        .EnableEvents = False
        .CutCopyMode = False
    End With
    Dim x As Long, lastrow As Long
    lastrow = Sheets("Workspace").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For x = 1 To lastrow
        Windows("M_BR" & x & " ACCNTS(P).xlsm").Activate
        Application.Run "'M_BR" & x & " ACCNTS(P).xlsm'!Data Export"
        ActiveWorkbook.Close
        Windows("M_BR" & x & " ACCNTS(P).xlsm").Activate
        ActiveWorkbook.Close
        Windows("Open Pull Files.xlsm").Activate
    Next x
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
        .EnableEvents = True
        .CutCopyMode = True
    End With
End Sub
 
Upvote 0
Thanks for the help, code works perfectly
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,639
Members
449,093
Latest member
Ahmad123098

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