Run time Error 1004 Debug. 400 while running the macro in the spreadsheet.

Lukaa90

New Member
Joined
Sep 15, 2016
Messages
6
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:

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?
 

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.
Hello and welcome to The Board.
It may be useful to know on which line the code is failing.
I get a compiler error on the last line because of the double-quote after "2017".
 
Upvote 0
Hello and welcome to The Board.
It may be useful to know on which line the code is failing.
I get a compiler error on the last line because of the double-quote after "2017".


Ok ignore this line of code for now:

Code:
[TABLE="width: 168"]
<tbody>[TR]
[TD]'Copies the range from the summary sheet and paste it to the template[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]  SummarySheet.Range("C9:N32").Copy[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]   Workbooks("S:\xxxxxxxx\Billings\Billings 2016-17\Billings 2016 - 2017"\Summary Spreadsheet.xls").Worksheets("Summary").Range("C9:N32").PasteSpecial Paste:=xlValues[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]End Sub[/TD]
[/TR]
</tbody>[/TABLE]

Runtime error comes out after macro went through all the files in the folder.

It is not going further to this line of code:
Code:
Call AutoFit on the destination sheet so that all
    ' data is readable.
    SummarySheet.Columns.AutoFit
 
Upvote 0
Hello and welcome to The Board.
It may be useful to know on which line the code is failing.
I get a compiler error on the last line because of the double-quote after "2017".

Thanks for the reply, appreciate it!

Ok ignore this line of code for now:

Code:
[TABLE="width: 168"]
<tbody>[TR]
[TD]'Copies the range from the summary sheet and paste it to the template[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]  SummarySheet.Range("C9:N32").Copy[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]   Workbooks("S:\xxxxxxxx\Billings\Billings 2016-17\Billings 2016 - 2017"\Summary Spreadsheet.xls").Worksheets("Summary").Range("C9:N32").PasteSpecial Paste:=xlValues[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]End Sub[/TD]
[/TR]
</tbody>[/TABLE]

Runtime error comes out after macro went through all the files in the folder.

It is not going further to this line of code:
Code:
Call AutoFit on the destination sheet so that all
    ' data is readable.
    SummarySheet.Columns.AutoFit
 
Upvote 0
I cannot get it to fail for me on that line, but I can only guess at the content of your files.
You could try removing the last file that is being processed (use "debug.print" to display the filenames as they are being processed) to see if it is that last file causing a problem.
I would also change the "display alerts" to comment while investigating (why set it to "false" twice?).
 
Upvote 0

Forum statistics

Threads
1,215,381
Messages
6,124,615
Members
449,175
Latest member
Anniewonder

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