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
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