您的文本文件包含一些Ansi編碼的其他字符。 並且需要刪除「=」,引用。這是分號是分號。 行分隔符是「;;;」三個分號(我的猜測)。
Sub openCsv()
Dim Wb As Workbook
Dim myPath As String, myFile As String
Dim newFile As String
Dim s As String
Dim vR(), vSplit, vSub
Dim n As Long, i As Long, j As Integer
myPath = ThisWorkbook.Path & "\" 'your path
myFile = "myTest.csv" 'your csv file
newFile = Split(myFile, ".")(0) & ".xlsx"
s = TransFromUtf_8(myPath & myFile)
s = Replace(s, "=", "")
s = Replace(s, Chr(34), "")
vSplit = Split(s, ";;;")
For i = 0 To UBound(vSplit)
n = n + 1
vSub = Split(vSplit(i), ";")
ReDim Preserve vR(1 To 30, 1 To n)
For j = 0 To UBound(vSub)
vR(j + 1, n) = vSub(j)
Next j
Next i
Set Wb = Workbooks.Add
With Wb
With .Sheets(1)
.Cells.NumberFormatLocal = "@"
.Range("a1").Resize(n, 30) = WorksheetFunction.Transpose(vR)
.Columns.AutoFit
End With
.SaveAs myPath & newFile
.Close (0)
End With
End Sub
Function TransFromUtf_8(myFile As String)
Dim objStream
Set objStream = CreateObject("ADODB.Stream")
With objStream
.Charset = "utf-8"
.Open
.LoadFromFile myFile
TransFromUtf_8 = .ReadText
.Close
End With
Set objStream = Nothing
End Function
如果您在Excel中打開CSV文件,它是否將數據分成列? –
嗨羅賓,是的,excel可以正常打開我的csv –