Import data and paste it after the last row

jeremiah_j2k

New Member
Joined
Oct 16, 2014
Messages
32
Hello,

Need some advise and your expertise to modify my macro so it will add the imported data after the last entry on my existing table. currently, the code copy and paste the data to row 2 of the Database sheet but I am keeping records to that table would just like to update it instead of overwriting the data that i have. Can you advise how to make the macro add the imported data to the last row of the Database sheet? Thanks in advance for your help.


VBA Code:
Sub Import_RAW()
    Application.ScreenUpdating = False
    'Selection.AutoFilter
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .ButtonName = "Import"
        .Filters.Clear
        '.Filters.Add "Excel Files", "*.csv"
        '.Filters.Add "CSV File", "*.xlsx"
        .Title = "Ticket Rating"
        .Show
        If .SelectedItems.Count Then
            strFileSelected = .SelectedItems(1)
        Else
            'MsgBox "Cancelled by user!"
            Exit Sub 'Ideally, should exit from the bottom of the sub-routine.
        End If
    End With
    'Worksheets("Database").Range("A2:q10000").ClearContents
    Range("AD1").Value = VBA.FileDateTime(strFileSelected) '& " +8 GMT"
 
    fncFileSelected = strFileSelected

    With Workbooks.Open(Filename:=fncFileSelected, ReadOnly:=True)
        .Sheets(1).Cells(1).End(xlToRight).Offset(, 2).Resize(, 17).Value = ThisWorkbook.Worksheets("Database").Range("A1:q1").Value
        .Sheets(1).Cells(1).CurrentRegion.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Sheets(1).Cells(1).End(xlToRight).Offset(, 2).CurrentRegion, Unique:=False
        With .Sheets(1).Cells(1).End(xlToRight).Offset(, 2).CurrentRegion
            ThisWorkbook.Worksheets("Database").Range("A1:q1").Resize(.Rows.Count, .Columns.Count).Value = .Value
        ActiveWorkbook.RefreshAll
        End With
        Workbooks(.Name).Close 0
        ActiveWorkbook.RefreshAll
   
       
    End With

End Sub
 

Attachments

  • 2022-05-21 11_26_11-TestForm_Save - Excel.png
    2022-05-21 11_26_11-TestForm_Save - Excel.png
    51.5 KB · Views: 20

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I haven't tested this but I think all you are after is to replace your output line with this:
VBA Code:
            ThisWorkbook.Worksheets("Database").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(.Rows.Count, .Columns.Count).Value = .Value
 
Upvote 0
Hi Alex,

Thank you for your feedback.. i replaced the output line, however, i the imported data is still overwriting the existing entries on the table on Database sheet instead of adding it to the end of the row
 
Upvote 0
If you want to Append the data to what is in the Database sheet, why are you clearing the Database sheet ?
VBA Code:
    Worksheets("Database").Range("A2:q10000").ClearContents
 
Upvote 0
ohh yeah i should have commented that out like in the macro in my initial post.. it's now pasting the imported data to the end of the row but it includes the column label as shown in the attached image
 

Attachments

  • 2022-05-21 20_58_34-TestForm_Import - Excel.png
    2022-05-21 20_58_34-TestForm_Import - Excel.png
    10.7 KB · Views: 16
Upvote 0
I wouldn't normally do it this way and its a bit ugly but you can just add offset 1 row to your code.
VBA Code:
With .Sheets(1).Cells(1).End(xlToRight).Offset(, 2).CurrentRegion.Offset(1)
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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