CSV Import - Runtime Error 1004

leeksleeks

Board Regular
Joined
Oct 31, 2013
Messages
96
Hi,

I have a problem when everytime I run my macro for importing and converting CSV files that I get a Runtime error 1004 saying that it cant find the text file etc. The thing is that if I record a Macro to import the data and copy and paste the path (which is exactly the same as the one I have in my macro) it works. Any ideas as to why this would work but when I run it when I open my file I get the Runtime 1004 error and the Refresh part is highlighted?

Code:
Sub CSV_Import()
Dim ws As Worksheet, strFile As String


Set ws = ActiveWorkbook.ActiveSheet


strFile = Dir("Z:\business\02 All\Business Reports\Raw Data\*.csv")
Do While Len(strFile) > 0
With ws.QueryTables.Add(Connection:="TEXT;" & strFile, Destination:=ws.Range("A1"))
     .TextFileParseType = xlDelimited
     .TextFileSemicolonDelimiter = True
     .Refresh
         
  End With

strFile = Dir
Loop
End Sub

Any help would be greatly appreciated. I think i have read every conceivable thread without a solution so far :eeek:
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Dir returns only the file name, without the parent folder path. Therefore you need:

Code:
With ws.QueryTables.Add(Connection:="TEXT;Z:\business\02 All\Business Reports\Raw Data\" & strFile, Destination:=ws.Range("A1"))
 
Upvote 0

Forum statistics

Threads
1,223,157
Messages
6,170,418
Members
452,325
Latest member
BlahQz

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