Opening a csv file to avoid corruption WITHOUT creating a query

nigelandrewfoster

Well-known Member
Joined
May 27, 2009
Messages
747
Hello

A csv file I need to open contains quite a number of foreign characters. By default, opening it in Excel corrupts them.

I have been using this to open it (successfully, without corruption):

Code:
Sub Open_CSV_Correctly()

    Dim wb As Workbook
    Dim strFile As String


    strFile = "E:\MyFile.csv"


    Set wb = Workbooks.Add
    
    With wb.Worksheets(1)
        With .QueryTables.Add(Connection:="TEXT;" & strFile, Destination:=.Range("A1"))
             .TextFileParseType = xlDelimited
             .TextFilePlatform = 65001
             .TextFileCommaDelimiter = True
             .Refresh
        End With
    End With
    
End Sub

However, it creates a query link, which I don't actually need.

Please can you tell me how I might achieve the same result, but without creating the link?

Thanks for your time

Nigel
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
If your only issue is the artifact query table and connection, you can simply delete those after the import is completed.

Rich (BB code):
Sub Open_CSV_Correctly()


    Dim wb As Workbook
    Dim strFile As String
    Dim c As WorkbookConnection
    Dim qt As QueryTable




    strFile = "\\freedomh.com\globaltpa-fs\Profile\A&G\jrowe\My Documents\MyFile.csv"




    Set wb = Workbooks.Add
    
    With wb.Worksheets(1)
        With .QueryTables.Add(Connection:="TEXT;" & strFile, Destination:=.Range("A1"))
             .TextFileParseType = xlDelimited
             .TextFilePlatform = 65001
             .TextFileCommaDelimiter = True
             .Refresh
        End With
    End With


    For Each c In wb.Connections
        c.Delete
    Next c
    For Each qt In wb.ActiveSheet.QueryTables
        qt.Delete
    Next qt
    
End Sub
 
Upvote 0
Sure, that will do nicely. Am completely new to query tables and manipulating them in VBA.

Very helpful, thank you very much.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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