What is the best code used to import data from a text/.csv file in Excel file without using: "Workbooks.Open" or "Query Tables". Also the number of rows in the text/.csv files varies largely and changes each day. Therefore, if you are using ADO or some other method, will you please include in your code a way to "Select * From" or "Copy All" without looping through each record?
Background/Reason
I am working in a very large environment where "Query Tables" and "Workbooks.open" code works on some machines and not others. (They are all using Excel 2010 and/or 2016 and claim to all have the same setup with Windows 7 and/or Windows 10.) I will take code that works on one machine and it won't work on another. The code is correct, the full path is defined with and/or without the extension; depending. (i.e., some machines require the extension in the path others will error 1004 if it is there and vice versa. There are also other random problems and the users won't send me a screenshot when it errors nor will they allow me to see/debug the errors on their machine.) Therefore, I would like to look at some solid alternatives as per above.
Then start the macro recorder, and edit the just imported query.
Just accept.
Stop the recorder.
The recorded code should look something like this.
Open in new window
Change Selection to the sheet specification, so you don't have to select the sheet to update.The sheet can also be hidden.
With a variable for the sheet like wsData, where wsData is assigned to the sheet with Set wsData= Worksheets("Data"), so the first line will be
With wsData.QueryTables(1)
The Connection in the recording will include the csv file name.
Using a variable CsvFileName having the complete path to the csv file, the file name can be changed by the code as shown.
Change the settings to not ask for filename on update.
Then you can update the query by running the code.
Formulas referring to the query will automatically adjust to the number of rows in the import.