Importing a .csv file

dmacmillan

Board Regular
Joined
Apr 5, 2004
Messages
125
Hi All,

For the purposes of refreshing the data On Open and OnTime is the data import wizard preferred method over VBA or vice-versa? Does it matter?

Thank you in advance,
David
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I am not sure I really understand the question. If you are using OnTime or Workbook_Open, you are using VBA. How would you manually use the Data Import Wizard in an automated setting?

By the way, if you record yourself using the Data Import Wizard manually, you will get the VBA code you need to do the import.
 
Upvote 0
Hi,

Thank you for responding and apologies for not making myself clear.

I guess the nub of question is, are there any downsides to conducting the refresh from VBA? Is it necessary to delete the connection On Close? If this is not done will connections be continuously added; does this matter?

Welcome your thoughts. Appreciate your consideration.

Kind regards,
David
 
Upvote 0
Its hard to say without seeing your code to know what it is actually doing and how it is connecting.
 
Upvote 0
Thanks for coming back ...

I will use this form of code:

Code:
' filename = CSV filename without directory (test.csv)
' outSheet = name of the worksheet in the current workbook
' where the data should go, will start in A1

Function doFileQuery(filename As String, outSheet As String) As Boolean
    Dim rootDir As String
    rootDir = "C:\myDirectory"
    Dim connectionName As String
    connectionName = "TEXT;" + rootDir + "\" + filename
    With Worksheets(outSheet).QueryTables.Add(Connection:=connectionName, Destination:=Worksheets(outSheet).Range("A1"))
        .Name = filename
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlOverwriteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .Refresh BackgroundQuery:=False
    End With
End Function

Kind regards,
David
 
Upvote 0
To be honest, I really don't know much about exactly how it all connects and whether connections should be closed. I have used the methodology you have mentioned, and I do sometimes find the connection messages quite annoying. Importing the data directly doesn't seem to have the same issues, so that is what I usually prefer (all things being equal).

Note that you can automate a data import as well, just the Macro Recorder and record yourself doing so to get the "bones" of that code.
 
Upvote 0
Joe,

Thank you for your response; food for thought that adds to the body of knowledge.

Kind regards,
David
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,476
Members
452,915
Latest member
hannnahheileen

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