VBA Import, Merge & Modify files

amaluna

New Member
Joined
Aug 4, 2015
Messages
33
Hello,
I have the following code which lets me pick multiple files from a specific folder and then merges all of them together. The code works great, except I ran into the issue where some files had a blank Column A and since the code is trying to find the last used row before pasting the new data, it's overwriting the previous data. Can someone help me modify the code so that when you select these files it removes column A and then pastes the data in the sheet I want?

Thank you in advance!

Code:
Sub select_merge()    Dim Cnt As Long
    Dim destCell As Range
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    Dim vrtSelectedItem As Variant
    Application.ScreenUpdating = False
 
    Sheets("input").UsedRange.ClearContents

    With fd
        If .Show = -1 Then
            For Each vrtSelectedItem In .SelectedItems
                Cnt = Cnt + 1
                If Cnt = 1 Then
                    Set destCell = Worksheets("input").Range("A1")
                Else
                    Set destCell = Worksheets("input").Cells _
                    (Rows.Count, "A").End(xlUp).Offset(1)
                End If
                
                If Cnt = 1 Then
                    With destCell.Parent.QueryTables.Add(Connection:="TEXT;" & _
                    vrtSelectedItem, Destination:=destCell)
                        .TextFileStartRow = 1
                        .TextFileParseType = xlDelimited
                        .TextFileCommaDelimiter = True
                        .Refresh BackgroundQuery:=False
                    End With
                Else
                    With destCell.Parent.QueryTables.Add(Connection:="TEXT;" & _
                    vrtSelectedItem, Destination:=destCell)
                        .TextFileStartRow = 2
                        .TextFileParseType = xlDelimited
                        .TextFileCommaDelimiter = True
                        .Refresh BackgroundQuery:=False
                    End With
                End If
            Next vrtSelectedItem

            If Cnt = 0 Then _
                MsgBox "No CSV files were found...", vbExclamation
        End If
    End With
    Set fd = Nothing
     
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
The code works great, except I ran into the issue where some files had a blank Column A and since the code is trying to find the last used row before pasting the new data
Is there some column, other than column A, you can use to locate the last row of data (in other words, is there some column that will ALWAYS be populated for every row with data)?
 
Upvote 0
Then try changing this line:
Code:
Set destCell = Worksheets("input").Cells _
                    (Rows.Count, "A").End(xlUp).Offset(1)
to this:
Code:
Set destCell = Worksheets("input").Cells _
                    (Rows.Count, "B").End(xlUp).Offset(1,-1)
 
Upvote 0

Forum statistics

Threads
1,215,005
Messages
6,122,661
Members
449,091
Latest member
peppernaut

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