Sub ImportCSVWithLeadingZeros()
Dim ws As Worksheet
Dim qt As QueryTable
Dim csvPath As String
' CSVファイルのフルパスを指定
csvPath = "C:\Users\YourName\Desktop\sample.csv"
' 新しいワークシートを作成
Set ws = Worksheets.Add
' クエリテーブルとしてCSVをインポート
Set qt = ws.QueryTables.Add(Connection:="TEXT;" & csvPath, Destination:=ws.Range("A1"))
With qt
.TextFileParseType = xlDelimited
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True ' CSVなのでカンマ区切り
.TextFilePlatform = xlWindows
' 各列のデータ型を文字列に設定(すべて1)
' 例:5列まで文字列として設定(必要に応じて増やす)
.TextFileColumnDataTypes = Array(2, 2, 2, 2, 2)
.Refresh BackgroundQuery:=False
End With
End Sub