VBA: Import every new data into a new column

Ouble

New Member
Joined
Nov 30, 2015
Messages
18
Hi all, in need of some help to import new data into new column. Currently, Ive only managed to import the data but with every new data, they are stacked below. I need them to be in a new column. Eg. data1 A2:B2, data2 D2:E2, etc...

Also, is it possible to import the filename of the import data at the row before the data? Eg. A1, D1, etc...

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.Range("A" & wsDst.Rows.Count).End(xlUp).Offset(2)
        ActiveSheet.UsedRange.Resize(, 2).Copy rngDst
        FN = Dir()
        ActiveWorkbook.Close False
    Loop
End Sub

Ouble
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
see if this works for you
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(1, Columns.Count).End(xlToLeft).Offset(0, 2)
        ActiveSheet.UsedRange.Resize(, 2).Copy rngDst
        wsDst.Cells(1, Colums.Count).End(xlToLeft).Offset(0, -1).Insert xlShiftDown
        wsDst.Cells(1, Colums.Count).End(xlToLeft).Offset(0, -1) = FN
        FN = Dir()
        ActiveWorkbook.Close False
    Loop
End Sub
 
Last edited:
Upvote 0
Thank JLGWhiz, there seemed to be a runtime error 'object required' on
wsDst.Cells(1, Colums.Count).End(xlToLeft).Offset(0, -1).Insert xlShiftDown
wsDst.Cells(1, Colums.Count).End(xlToLeft).Offset(0, -1) = FN

any idea how? Thanks

Ouble
 
Upvote 0
Thank JLGWhiz, there seemed to be a runtime error 'object required' on
wsDst.Cells(1, Colums.Count).End(xlToLeft).Offset(0, -1).Insert xlShiftDown
wsDst.Cells(1, Colums.Count).End(xlToLeft).Offset(0, -1) = FN

any idea how? Thanks

Ouble
Columns is misspelled. Insert the 'n' in both places and it should work OK.
 
Upvote 0
Thanks thanks!

Ive edited it to suit my format. Thanks for the help.

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
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,847
Members
452,361
Latest member
d3ad3y3

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