Sub utf8import()
Dim ws As Worksheet
Dim qtbl As QueryTable
Dim getfilepath As String
Set ws = ActiveSheet
getfilepath = Application.GetOpenFilename("ブック, *.txt") ' .txtファイルを選ぶ
If getfilepath <> "False" Then
Else
MsgBox "キャンセル"
End
End If
Set qtbl = ws.QueryTables.Add(Connection:="TEXT;" & getfilepath, Destination:=ws.Range("A1")) ' CSV を開く
With qtbl
.TextFilePlatform = 65001 ' 文字コード UTF-8
.TextFileTabDelimiter = True ' タブ区切り
.RefreshStyle = xlOverwriteCells ' 上書き
.Refresh ' 表示する
.Delete ' CSV接続 解除
End With
End Sub
UTF-8 LF改行の場合のタブ区切り
Sub Sample2()
Dim buf As String, Target As String, i As Long
Dim tmp1 As Variant, tmp2 As Variant, j As Long
Target = “C:\商品レポート.txt”
With CreateObject(“ADODB.Stream”)
.Charset = “UTF-8”
.Open
.LoadFromFile Target
buf = .ReadText
.Close
tmp1 = Split(buf, vbLf) ‘LFはvbLf CR + LFはvbCrLf
For i = 0 To UBound(tmp1)
tmp2 = Split(tmp1(i), vbTab) ‘タブはvbTab
j = j + 1
Range(Cells(j, 1), Cells(j, 34)) = tmp2
Next i
End With
End Sub
カンマ区切り
Sub utf8importcsv()
Dim ws As Worksheet
Dim qtbl As QueryTable
Dim getfilepath As String
Set ws = ActiveSheet
getfilepath = Application.GetOpenFilename("ブック, *.csv") ' .csvファイルを選ぶ
If getfilepath <> "False" Then
Else
MsgBox "キャンセル"
End
End If
Set qtbl = ws.QueryTables.Add(Connection:="TEXT;" & getfilepath, Destination:=ws.Range("A1")) ' CSV を開く
With qtbl
.TextFilePlatform = 65001 ' 文字コード UTF-8
.TextFileCommaDelimiter = True ' カンマ区切り
.RefreshStyle = xlOverwriteCells ' 上書き
.Refresh ' 表示する
.Delete ' CSV接続 解除
End With
End Sub