TextToColumnsメソッドのFieldInfoを可変にする | 備忘録 (。・_・。)ノ

Option Explicit
Sub import()
Dim i As Long
Dim j As Long
Dim lonFile As Long
Dim strFile As String
Dim ranPos As Range
Dim ranTmp As Range
Dim strText As String
Dim valInfo() As Variant

j = 6
ReDim valInfo(1 To j, 0 To 1)

strFile = "C:\ak\down\aaa.csv"
Worksheets(1).Activate

Range("A1").CurrentRegion.Select
Selection.Clear

Set ranPos = Range("A1")
lonFile = FreeFile
Open strFile For Input As #lonFile
i = 0
Do Until EOF(lonFile)
Line Input #lonFile, strText
ranPos.Offset(i).Value = strText
i = i + 1
Loop
Close #lonFile

For i = 1 To j
valInfo(i, 0) = i
valInfo(i, 1) = 2
Next i

Set ranTmp = Range(ranPos, ranPos.End(xlDown))
'ranTmp.Replace What:="'", Replacement:=""
ranTmp.TextToColumns DataType:=xlDelimited, Comma:=True, FieldInfo:=valInfo
Cells.Select
Cells.EntireColumn.AutoFit
ranPos.Cells.Select
Set ranPos = Nothing
Set ranTmp = Nothing

End Sub