Hi,
I am receiving blank 400 error each time I am running the macro. If I am using F8 in debug mode it gives me an error 1004.
What my macro does, it creates the summary sheet and copying the table from all the spreadsheets in the folder. that table is in every spreadsheet in the same location B7:N32. Therefore it consolidates about 14 tables from 14 workbooks into one summary table.
Code is:
Would you be so kind and help me with that?
I am receiving blank 400 error each time I am running the macro. If I am using F8 in debug mode it gives me an error 1004.
What my macro does, it creates the summary sheet and copying the table from all the spreadsheets in the folder. that table is in every spreadsheet in the same location B7:N32. Therefore it consolidates about 14 tables from 14 workbooks into one summary table.
Code is:
Code:
Sub MergeAllWorkbooks()
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim NRow As Long
Dim FileName As String
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range
' Create a new workbook and set a variable to the first sheet.
Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
' Modify this folder path to point to the files you want to use.
FolderPath = "S:\OPERATIONS\xxxxxxxxxx\Billings\Billings 2016-17\Billings 2016 - 2017\PRS MR&A\"
' NRow keeps track of where to insert new rows in the destination workbook.
NRow = 1
' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "*.xlsm*")
' Loop until Dir returns an empty string.
Do While FileName <> ""
' Open a workbook in the folder
Set WorkBk = Workbooks.Open(FolderPath & FileName)
' Set the source range to be A9 through C9.
' Modify this range for your workbooks.
' It can span multiple rows.
Set SourceRange = WorkBk.Worksheets(1).Range("B7:N32")
' Set the destination range to start at column B and
' be the same size as the source range.
Set DestRange = SummarySheet.Range("B7:N32")
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)
'Set Tablerange = SummarySheet.Range("B7:B32,B7:N7")
'Copy over the values from the source to the destination.
'Tablerange.Value = SourceRange.Value
Application.DisplayAlerts = False
SourceRange.Copy
DestRange.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlAdd
DestRange.PasteSpecial Paste:=xlPasteFormats
Application.DisplayAlerts = False
' Increase NRow so that we know where to copy data next.
' NRow = NRow
' Close the source workbook without saving changes.
WorkBk.Close savechanges:=False
' Use Dir to get the next file name.
FileName = Dir()
Loop
' Call AutoFit on the destination sheet so that all
' data is readable.
SummarySheet.Columns.AutoFit
'Copies the range from the summary sheet and paste it to the template
SummarySheet.Range("C9:N32").Copy
Workbooks("S:\xxxxxxxx\Billings\Billings 2016-17\Billings 2016 - 2017"\Summary Spreadsheet.xls").Worksheets("Summary").Range("C9:N32").PasteSpecial Paste:=xlValues
End Sub
Would you be so kind and help me with that?