'''
''' Use a QueryTable object to open a CSV file as text to avoid having Excel guess about cell format based on the contents.
'''
Sub CSV_Open_Text(FilePath As String)
Dim ColCnt As Long, FormatArray() As Long, I As Long
Dim QT As QueryTable
Dim QueryConnection As String, QueryName As String
Dim WB As Workbook
Dim WS As Worksheet
QueryConnection = "TEXT;" & FilePath
QueryName = "CSV_Import"
Set WB = Application.Workbooks.Add
Set WS = WB.Worksheets(1)
For I = 1 To WB.Worksheets.Count
If I >= 2 Then
Application.DisplayAlerts = False
WB.Worksheets(I).Delete
Application.DisplayAlerts = True
End If
Next I
'Create a QueryTable
Set QT = WS.QueryTables.Add(Connection:=QueryConnection, Destination:=Range("A1"))
'Set QueryTable properties
With QT
.Name = QueryName
.BackgroundQuery = False
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = xlWindows
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
End With
QT.Refresh 'intial file load, general format
ColCnt = ActiveSheet.UsedRange.Columns.Count 'count imported columns
'Create query format array
ReDim FormatArray(ColCnt - 1)
For I = 0 To ColCnt - 1
FormatArray(I) = xlTextFormat
Next I
QT.TextFileColumnDataTypes = FormatArray 'Set query format array
QT.Refresh 'reload as text format
WS.Name = Split(FilePath, "\")(UBound(Split(FilePath, "\")))
End Sub