Amend Macro not to close current workbook

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,666
Office Version
  1. 2019
Platform
  1. Windows
I have the following code below to export files

When running the macro, the current workbook is being closed


I would like my code amended so that the current workbook is not closed


It would be appreciated if someone could kindly assist me


Code:
 Sub Export_Files()
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.CutCopyMode = False
End With

Dim ws As Worksheet
Dim rngCell As Range
Dim fPATH As String:    fPATH = "C:\pull\"
Dim FSO As Object: Set FSO = CreateObject("Scripting.FileSystemObject")
Dim FLD As Object: Set FLD = FSO.GetFolder(fPATH)
Dim SubFLDRS As Object: Set SubFLDRS = FLD.SubFolders
Dim SubFLD As Object

Dim wbData As Workbook
Set ws = ThisWorkbook.Sheets("Workbooks")

Application.ScreenUpdating = False
On Error Resume Next
For Each SubFLD In SubFLDRS

For Each rngCell In ws.Range("A1:A" & ws.Cells(ws.Rows.Count, 1).End(xlUp).Row)
Set wbData = Workbooks.Open(SubFLD & "\" & rngCell.Value)
'Application.Run ("'" + rngCell.Value + "'!" + Workbooks(rngCell.Value).Sheets("workbooks").[d1].Value) 'run macros
Application.Run ("'" + rngCell.Value + "'!" + ThisWorkbook.Sheets("workbooks").[d1].Value)

wbData.Close 'close the file
Next rngCell

 Next SubFLD

    With Application
.ScreenUpdating = True
.DisplayAlerts = True
.CutCopyMode = False
End With

End Sub
 

Some videos you may like

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Puertorekinsam

Active Member
Joined
Oct 8, 2005
Messages
287
VBA Code:
Sub Export_Files()
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.CutCopyMode = False
End With

Dim ws As Worksheet
Dim rngCell As Range
Dim fPATH As String:    fPATH = "C:\pull\"
Dim FSO As Object: Set FSO = CreateObject("Scripting.FileSystemObject")
Dim FLD As Object: Set FLD = FSO.GetFolder(fPATH)
Dim SubFLDRS As Object: Set SubFLDRS = FLD.SubFolders
Dim SubFLD As Object

Dim wbData As Workbook
Set ws = ThisWorkbook.Sheets("Workbooks")

Application.ScreenUpdating = False
On Error Resume Next
For Each SubFLD In SubFLDRS

For Each rngCell In ws.Range("A1:A" & ws.Cells(ws.Rows.Count, 1).End(xlUp).Row)
Set wbData = Workbooks.Open(SubFLD & "\" & rngCell.Value)
'Application.Run ("'" + rngCell.Value + "'!" + Workbooks(rngCell.Value).Sheets("workbooks").[d1].Value) 'run macros
Application.Run ("'" + rngCell.Value + "'!" + ThisWorkbook.Sheets("workbooks").[d1].Value)

if wbdata.name <> thisworkbook.name then wbData.Close 'close the file
Next rngCell

Next SubFLD

    With Application
.ScreenUpdating = True
.DisplayAlerts = True
.CutCopyMode = False
End With

End Sub
 

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,666
Office Version
  1. 2019
Platform
  1. Windows
Thanks for the help, much appreciated
 

Watch MrExcel Video

Forum statistics

Threads
1,114,419
Messages
5,547,819
Members
410,813
Latest member
Vhinzvirgo
Top