VBA: copy import file data below current data

Yzori

New Member
Joined
Jun 17, 2014
Messages
5
Hi guys,

I am relatively new to VBA, and I am getting stuck with my code. What I want to achieve is to import a file on a daily basis and copy that data in another worksheet below the existing data. I keep getting the following error: "Runtime error: 1004. We can't copy because the Copy and Paste area are not the same."

I got the following code so far:

Code:
Sub ImportFile()


' Dimensions 
    Dim myPath As String
    Dim folderPath As String
    Dim Filename As String
    Dim LastRow As Long
    Dim LastColumn As Long
    Dim sht As Worksheet
    Dim StartCell As Range
    
    Set sht = Worksheets("raw data")
    Set StartCell = Range("A1")


' Specifications of file paths
    folderPath = Application.ActiveWorkbook.Path
    myPath = Application.ActiveWorkbook.FullName
    Filename = Application.GetOpenFilename("Supership Import, *.csv")
    
'Refresh UsedRange
    Worksheets("raw data").UsedRange
    
' Find Last Row and Column
    LastRow = StartCell.SpecialCells(xlCellTypeLastCell).Row
    LastColumn = StartCell.SpecialCells(xlCellTypeLastCell).Column
    
' Open Workbook
    Workbooks.Open Filename:=Filename
    Columns("G:G").Select
    Selection.Insert Shift:=xlToRight
    Selection.Insert Shift:=xlToRight
    Columns("J:J").Select
    Selection.TextToColumns Destination:=Range("G1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _
        Semicolon:=False, Comma:=True, Space:=True, Other:=False, FieldInfo:= _
        Array(Array(1, 1), Array(2, 2)), TrailingMinusNumbers:=True
    Range("A:AO").Copy Destination:=StartCell(LastRow + 1, LastColumn)
    Windows("Module - US.xlsm").Activate
End Sub
Any help would be greatly appreciated.
 
Last edited:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
With this line you are copying entire columns (part in red), but then trying to copy them to a range starting 1 row below the last used row.(in blue)
ie trying to copy 1048576 rows, but paste those rows starting in (say) row22
Code:
[COLOR=#ff0000]Range("A:AO").Copy[/COLOR] Destination:=[COLOR=#0000ff]StartCell(LastRow + 1, LastColumn)[/COLOR]
try
Code:
Range("A1:AO"& LastRow).Copy Destination:=StartCell(LastRow + 1, LastColumn)
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,620
Latest member
dsubash

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