Trying to copy from CSV file - Run-time error '1004'

DanGK1

Board Regular
Joined
Jul 7, 2010
Messages
60
Hi

I am attempting to open a csv file and then copy data from it to my main workbook. I can open the file fine but when I get to the bit of code that requests to copy the data, I get Run-time error '1004'; Application-defined or object-defined error and I cannot work out why. As far as Excel appears to be concerned, the csv file is the active workbook.

Code:
Sub select_report()
Dim MyFile As String
ChDir "C:\Users\******\Downloads"
MyFile = Application.GetOpenFilename("Files (*.csv),*.csv", , "Select Report File")
Workbooks.Open (MyFile)
ActiveSheet.Name = "report"
' check the active workbook
   MsgBox ActiveWorkbook.Name, vbInformation, "Workbook Name"
With Sheets("report")
    .Range("A2:U" & Lastrow).Copy
End With
ActiveWorkbook.Close
With Sheets("SFData")
    .Range("A2").Paste
End With
End Sub

Any advice or suggestions please?

Thanks
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You haven't defined LastRow I think.

Code:
[COLOR=#00007F][FONT='inherit']Dim[/FONT][/COLOR][COLOR=#3D3D3D][FONT='inherit'] LastRow [/FONT][/COLOR][COLOR=#00007F][FONT='inherit']As[/FONT][/COLOR][COLOR=#00007F][FONT='inherit']Long[/FONT][/COLOR]

Then you have to set it to use it...
 
Last edited:
Upvote 0
thanks, that was not quite it, but pointed me to the right place

Code:
Lastrow = Range("A:U").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 
Upvote 0
thanks, that was not quite it, but pointed me to the right place

Code:
Lastrow = Range("A:U").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

No, like I said you have to give it a value. Like you then did. Great!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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