Unable to close previous workbook

home_toast

New Member
Joined
Mar 30, 2009
Messages
36
Hi guys,

I am having trouble with the following code that I have been writing. I am not sure how to close the previous workbook.

Sub openFiles()

Dim FileDir As Variant
Dim FileName As Variant
Dim xName As String

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

xlsFileDir = "H:\InternalFiles\data\test\"
xlsFileName = Dir(xlsFileDir & "*.xls")
Do While xlsFileName <> ""
Workbooks.Open Filename:=xlsFileDir & xlsFileName, _
UpdateLinks:=False
xName = Dir("*.xls") 'Not sure if this is the correct way to do it.
Cells.Select
Selection.Copy
Windows("Data_Project.xlsm").Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Cells.EntireColumn.AutoFit
Sheets.Add After:=Sheets(Sheets.Count)
Windows(xName).Activate 'This part is not working. I cannot close the previous workbook.
ActiveWorkbook.Close
xlsFileName = Dir
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub

I am greatful for any solutions. Thanks.

Regards,
TJ
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try this (untested):-
Code:
Sub openFiles()
    Dim FileDir As Variant, FileName As Variant, xName As String, file_to_open As Workbook
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    xlsFileDir = "H:\InternalFiles\data\test\"
    xlsFileName = Dir(xlsFileDir & "*.xls")
    Do While xlsFileName <> ""
        Set file_to_open = Workbooks.Open(xlsFileDir & xlsFileName, UpdateLinks:=False)
        Cells.Copy
        ThisWorkbook.Activate
        Selection.PasteSpecial (xlPasteValues)
        Application.CutCopyMode = False
        Cells.EntireColumn.AutoFit
        Sheets.Add After:=Sheets(Sheets.Count)
        file_to_open.Close False 'closes workbook without saving changes
        xlsFileName = Dir
    Loop
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True
End Sub
 
Upvote 0
Try this (untested):-
Code:
Sub openFiles()
    Dim FileDir As Variant, FileName As Variant, xName As String, file_to_open As Workbook
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    xlsFileDir = "H:\InternalFiles\data\test\"
    xlsFileName = Dir(xlsFileDir & "*.xls")
    Do While xlsFileName <> ""
        Set file_to_open = Workbooks.Open(xlsFileDir & xlsFileName, UpdateLinks:=False)
        Cells.Copy
        ThisWorkbook.Activate
        Selection.PasteSpecial (xlPasteValues)
        Application.CutCopyMode = False
        Cells.EntireColumn.AutoFit
        Sheets.Add After:=Sheets(Sheets.Count)
        file_to_open.Close False 'closes workbook without saving changes
        xlsFileName = Dir
    Loop
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True
End Sub

Hi Richard,

Made some slight changes to the VBA code but it works perfectly.

Thanks.

Regards,
TJ
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,286
Members
452,902
Latest member
Knuddeluff

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