Excel VBA importing multiple excel files from a folder

Tomvba

New Member
Joined
Feb 20, 2019
Messages
2
Hello,


I'm trying to import a random ammount of excel files into a new excel file.

I used the vba code from this thread as a base: https://www.mrexcel.com/forum/excel...-vba-importing-multiple-csv-files-folder.html

Code:
Sub ImportCSVData()

Dim wb As Workbook
Dim wbCSV As Workbook
Dim myPath As String
Dim myFile As Variant
Dim fileType As String
Dim i As Integer


 With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select Source Folder"
        .AllowMultiSelect = False
        .Show

        myPath = .SelectedItems(1) & "\"
    End With


  fileType = "*.xlsx*"


  myFile = Dir(myPath & fileType)


 Workbooks.Add

 ActiveWorkbook.SaveAs fileName:= _
        myPath & "Total Results.xlsm", FileFormat:= _
        xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

Set wb = Workbooks.Open(myPath & "Total Results.xlsm")


  Do While myFile <> ""
    Worksheets.Add(Before:=Worksheets("Blad1")).Name = "bestand " & i + 1

    With ActiveSheet.QueryTables.Add(Connection:="TEXT" & myPath & myFile _
            , Destination:=ActiveSheet.Range("$A$1"))
            .Name = myFile
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 850
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
            1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False    'Error occurs here!
        End With
    i = i + 1
    myFile = Dir
    
  Loop

  MsgBox "Result Import Complete"

End Sub



This works great with csv-files. If i try adapting it to xlsx-files, i get an error in this line:

Code:
With ActiveSheet.QueryTables.Add(Connection:="TEXT" & myPath & myFile _

How can i make it work? I think it has something to do with the TEXT -part of the code.

Thank you for your help.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
The QueryTables TEXT import method (Data tab > From Text) only works with text files. To import .xlsx files the easiest method is Workbooks.Open, to open the source workbook, then copy/import the required range. Other methods are Microsoft Query and Power Query, however they both import the data as a table.

For an example of importing with Workbooks.Open, see https://www.mrexcel.com/forum/excel...itional-variable-post5229226.html#post5229226
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,575
Members
449,039
Latest member
Arbind kumar

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