Hi All;
I found the following code online and it does what I generally need. There is a minor feature that I'd like to disable though. If you delete all of the cells in "data" I get a warning message:
Instead of linking the data in the spreadsheet to the .csv file permanently, is there a way to just import the csv file data and break the link?
Also, if the user cancels the file selection, the code errors out.
Thanks,
Mike
I found the following code online and it does what I generally need. There is a minor feature that I'd like to disable though. If you delete all of the cells in "data" I get a warning message:
"The range you deleted is associated with a query that retrieves data from an external source. Do you want to delete the query in addition to the range? If you click No, the query will retrieve new data to the worksheet the next time the query is refreshed."
Instead of linking the data in the spreadsheet to the .csv file permanently, is there a way to just import the csv file data and break the link?
Also, if the user cancels the file selection, the code errors out.
Thanks,
Mike
Code:
Sub load_csv()
MsgBox "Please Select the .CSV File to be Analysed"
Dim ws As Worksheet, strFile As String
Set ws = ActiveWorkbook.Sheets("data") 'set to data sheet
strFile = Application.GetOpenFilename("Text Files (*.csv),*.csv", , "Please select text file...")
With ws.QueryTables.Add(Connection:="TEXT;" & strFile, Destination:=ws.Range("A1"))
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.Refresh
End With
MsgBox "The CSV file has loaded sucessfully."
End Sub