Hello,
Hope you all doing well and staying far far away from COVID 19.
What i'm trying to do is to copy all the tabs of all the workbooks in a folder into a single workbook. But I have 2 errors :
one is from the statement:
Set nusheet = Workbooks(basebook).Worksheets.Add.
But if I take 'ThisWorkbook' instead of "Workbooks(basebook)" then fine. I just cannot figure out why 'basebook' is not working.
The other one is from the statement:
oFile.Close SaveChanges:=False, I have a '438' for it: object does not support the method or property. I need the statement since I don't want all the books open after I finish using them.
Thanks in advance.
Sub CopyMultiBooksInOne()
Dim strPath As String
Dim oFSO As Object
Dim oFile As Object
Dim oFolder As Object
Dim obook As Object
strPath = "C:\Users\pl04512\Documents\pnc\Franktest\Aja\CRE"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(strPath)
basebook = "Book1.xlsx"
For Each oFile In oFolder.Files
If oFile.Name Like "*.xlsx" Then
Set obook = Workbooks.Open(oFile)
For Each st In obook.Worksheets
Set nusheet = Workbooks(basebook).Worksheets.Add
nusheet.Name = st.Name
' st.UsedRange.Copy Workbooks(basebook).Worksheets(st.Name).Cells(1, 1)
' Workbooks(basebook).Worksheets(st.Name).UsedRange.EntireColumn.AutoFit
Next
End If
oFile.Close SaveChanges:=False
Next
Set oFSO = Nothing
Set oFile = Nothing
Set oFolder = Nothing
End Sub
Hope you all doing well and staying far far away from COVID 19.
What i'm trying to do is to copy all the tabs of all the workbooks in a folder into a single workbook. But I have 2 errors :
one is from the statement:
Set nusheet = Workbooks(basebook).Worksheets.Add.
But if I take 'ThisWorkbook' instead of "Workbooks(basebook)" then fine. I just cannot figure out why 'basebook' is not working.
The other one is from the statement:
oFile.Close SaveChanges:=False, I have a '438' for it: object does not support the method or property. I need the statement since I don't want all the books open after I finish using them.
Thanks in advance.
Sub CopyMultiBooksInOne()
Dim strPath As String
Dim oFSO As Object
Dim oFile As Object
Dim oFolder As Object
Dim obook As Object
strPath = "C:\Users\pl04512\Documents\pnc\Franktest\Aja\CRE"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(strPath)
basebook = "Book1.xlsx"
For Each oFile In oFolder.Files
If oFile.Name Like "*.xlsx" Then
Set obook = Workbooks.Open(oFile)
For Each st In obook.Worksheets
Set nusheet = Workbooks(basebook).Worksheets.Add
nusheet.Name = st.Name
' st.UsedRange.Copy Workbooks(basebook).Worksheets(st.Name).Cells(1, 1)
' Workbooks(basebook).Worksheets(st.Name).UsedRange.EntireColumn.AutoFit
Next
End If
oFile.Close SaveChanges:=False
Next
Set oFSO = Nothing
Set oFile = Nothing
Set oFolder = Nothing
End Sub