VBA: Import every 5 .xls data into a new worksheet

Ouble

New Member
Joined
Nov 30, 2015
Messages
18
Hi all, Ive got the coding more or less down. However, im trying to figure out if excel could import the 1st 5 excel file's data to one Sheet 1 then the next 5 to Sheet 2, etc...

Currently, its importing everything into one sheet.

Any help is greatly appreciated. Thanks!

Code:
Sub ImportFiles()


    Dim Fldr As String, FN As String
    Dim wsDst As Worksheet, rngDst As Range
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count = 0 Then
            Exit Sub
        End If
        Fldr = .SelectedItems(1)
    End With
    
    Set wsDst = ThisWorkbook.Sheets("Sheet1")
    FN = Dir(Fldr & "\*.xls", vbNormal)
    
    Do While FN <> ""
        Workbooks.OpenText Filename:=Fldr & "\" & FN, Space:=False
        Set rngDst = wsDst.Cells(2, Columns.Count).End(xlToLeft).Offset(0, 2)
        ActiveSheet.UsedRange.Resize(, 2).Copy rngDst
        wsDst.Cells(2, Columns.Count).End(xlToLeft).Offset(0, -2).Insert xlShiftDown
        wsDst.Cells(2, Columns.Count).End(xlToLeft).Offset(-1, -1) = FN
        
        FN = Dir()
        ActiveWorkbook.Close False
    
    Loop
End Sub

Ouble
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I haven't looked carefully at all your code so this may need a tweak. I assumed the sheet names are Sheet1, Sheet2, ..., and that the sheets needed are already in place and ready to accept the output of the macro.
Rich (BB code):
    Dim Fldr As String, FN As String
    Dim wsDst As Worksheet, rngDst As Range
    Dim ct As Long, N As Long
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count = 0 Then
            Exit Sub
        End If
        Fldr = .SelectedItems(1)
    End With
    
    FN = Dir(Fldr & "\*.xls", vbNormal)
    N = 1
    Do While FN <> ""
        ct = ct + 1
        If ct Mod 6 = 0 Then N = N + 1
        Set wsDst = ThisWorkbook.Sheets("Sheet" & N)
        Workbooks.OpenText Filename:=Fldr & "\" & FN, Space:=False
        Set rngDst = wsDst.Cells(2, Columns.Count).End(xlToLeft).Offset(0, 2)
        ActiveSheet.UsedRange.Resize(, 2).Copy rngDst
        wsDst.Cells(2, Columns.Count).End(xlToLeft).Offset(0, -2).Insert xlShiftDown
        wsDst.Cells(2, Columns.Count).End(xlToLeft).Offset(-1, -1) = FN
        
        FN = Dir()
        ActiveWorkbook.Close False
    
    Loop
End Sub
 
Upvote 0
Hi Joe,

Currently, sheet 1 is showing 5 results. The following sheet, sheet 2, sheet 3, etc... its importing 6 results.

Therefore, Im trying to play around with the code below but with no luck. What should I do?
ct = ct + 1
If ct Mod 6 = 0

Ouble
 
Upvote 0
Hi Joe,

Currently, sheet 1 is showing 5 results. The following sheet, sheet 2, sheet 3, etc... its importing 6 results.

Therefore, Im trying to play around with the code below but with no luck. What should I do?
ct = ct + 1
If ct Mod 6 = 0

Ouble
Here's a revision.
Code:
Sub ImportFiles()
Dim Fldr As String, FN As String
    Dim wsDst As Worksheet, rngDst As Range
    Dim ct As Long, N As Long
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count = 0 Then
            Exit Sub
        End If
        Fldr = .SelectedItems(1)
    End With
    
    FN = Dir(Fldr & "\*.xls", vbNormal)
    N = 1
    Do While FN <> ""
        ct = ct + 1
        Set wsDst = ThisWorkbook.Sheets("Sheet" & N)
        Workbooks.OpenText Filename:=Fldr & "\" & FN, Space:=False
        Set rngDst = wsDst.Cells(2, Columns.Count).End(xlToLeft).Offset(0, 2)
        ActiveSheet.UsedRange.Resize(, 2).Copy rngDst
        wsDst.Cells(2, Columns.Count).End(xlToLeft).Offset(0, -2).Insert xlShiftDown
        wsDst.Cells(2, Columns.Count).End(xlToLeft).Offset(-1, -1) = FN
        If ct Mod 5 = 0 Then N = N + 1
        FN = Dir()
        ActiveWorkbook.Close False
    
    Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,444
Messages
6,124,893
Members
449,194
Latest member
JayEggleton

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