I'm trying to import Csv file to excel using VBA code, I want it to work with all Csv files but it didn't. Here is my code:
Sub Input_CSV()
Dim Wb As String
Dim Arr
Set Wb = GetObject(Application.GetOpenFilename("csv file,*.csv", , "please choose a csv file", , False))
Dim blnImportData As Boolean
blnImportData = ImportCSV(Wb, "Sheet1", "A1")
If blnImportData Then MsgBox "Import CSV process complete", vbInformation, ThisWorkbook.Name _
Else MsgBox "Import CSV process failed", vbCritical, ThisWorkbook.Name
End Sub
And here is the function code
Function ImportCSV(ByVal Filename As String, _
ByVal Worksheet As String, _
ByVal StartCell As String) As Boolean
On Error GoTo Catch
Dim strConnectionName As String
strConnectionName = "TEXT;" + Filename
With Worksheets(Worksheet).QueryTables.Add(Connection:=strConnectionName, _
Destination:=Worksheets(Worksheet).Range(StartCell))
.Name = Filename
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True 'False
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True 'False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.Refresh BackgroundQuery:=False
End With
ImportCSV = True
Exit Function
Catch:
ImportCSV = False
End Function
How could I correct it? Could someone help me plz!