0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

a

Posted at

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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?