Importing multiple excel files into one spreadsheet

Woofy_McWoof_Woof

New Member
Joined
Oct 7, 2016
Messages
34
Yes, i think so. I am now getting the run time error 1004 after i moved the code into a module. When i debug it, it highlights With Workbooks.Open(Fileobj). Sorry for all of this.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,245
Office Version
365
Platform
Windows
What is the error message?
 

Woofy_McWoof_Woof

New Member
Joined
Oct 7, 2016
Messages
34
Run time error 1004 - Excel cannot access the file (path name and file name). There are several possible reasons:
The file name or path does not exist
The file is being use another programme
The workbook you are trying to save has the same name as a currently open workbook
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,245
Office Version
365
Platform
Windows
When you get the error, click debug & hover the mouse over the Fileobj variable.
A tool tip should appear showing the value of the variable, What does it say?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,245
Office Version
365
Platform
Windows
Is that the name of the file with the code?
 

Woofy_McWoof_Woof

New Member
Joined
Oct 7, 2016
Messages
34
The name of the file with the code is Import files test.xlsm which i have referenced in the code If Fileobj.Name <> "Import files test.xlsm" Then so I'm not sure why it adds in the extra~$
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,245
Office Version
365
Platform
Windows
Ok, how about
Code:
Sub Woofy()
    Dim Pth As String, Fname As String
    Dim Ws1 As Worksheet, ws2 As Worksheet
    
    Application.ScreenUpdating = False
    Set Ws1 = ThisWorkbook.Sheets("Optimised")
    Set ws2 = ThisWorkbook.Sheets("Baseload")
    Pth = "H:\test\"
    Fname = Dir(Pth & "*.xls*")
    Do While Fname <> ""
        If Fname <> "Import files test.xlsm" Then
            With Workbooks.Open(Pth & Fname)
                With .Sheets("Optimised")
                    .Range("A3:AC" & .Range("A" & .Rows.Count).End(xlUp).Row).Copy
                    Ws1.Range("A" & Ws1.Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
                End With
                With .Sheets("Baseload")
                    .Range("A3:AC" & .Range("A" & .Rows.Count).End(xlUp).Row).Copy
                    ws2.Range("A" & ws2.Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
                End With
                Application.CutCopyMode = False
                .Close False
            End With
        End If
        Fname = Dir
    Loop
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,245
Office Version
365
Platform
Windows
You're welcome & thanks for the feedback
 

Forum statistics

Threads
1,082,065
Messages
5,362,976
Members
400,702
Latest member
oliviaalx

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top