CSV file not importing unless opened and saved

UNKGuy

New Member
Joined
Apr 29, 2014
Messages
2
I have a macro that is supposed to import a downloaded csv file into a worksheet in my primary workbook. Currently, it only imports the csv file if it has been opened and saved before importing. I apologize in advance if this has been answered in previous posts, but I couldn't find the right keywords to find the information I was looking. The code below is what I am using.

Sub Import_DX()

Dim f as Variant

f = Application.GetOpenFilename(filefilter:="CSV Files (*.csv), *.csv")

If f = "False" Then Exit Sub

With Worksheets("DX Import").QueryTables.Add(Connection:= _
"TEXT;" & f, Destination:=Worksheets("DX Import").Range("$A$1"))
.Name = "DXI"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(9, 1, 1, 9, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With

End Sub

Respectfully
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi and welcome to MrExcel board!
Not sure but try: .RefreshOnFileOpen = True
Hope this helps
 
Upvote 0
Vladimir,

Thank you for the response. I changed it to true, but it prompts for prior connections. On another project I was working on I was able to make the connection persistent and that worked out well, but with this one, the downloaded csv will have a different name each day, and possibly saved in a different place depending on who downloads it. I have no idea why I would need to open and save the csv before it imports. Any other thoughts?

Respectfully
 
Upvote 0
...I have no idea why I would need to open and save the csv before it imports. Any other thoughts?
Usually resaving is required for downloaded files in Protected View mode.
Try to disable Protected View for a while to check it is the reason.
For that purpose in Excel - File - Options - Trust Center - Trust Center Settings - Protected View
try to uncheck all checkboxes.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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