Sub notmojibake()
Dim sh1 As Worksheet
Set sh1 = Worksheets(“取込シート”) ‘CSVデータを取込用シート
sh1.Range(“A1”).CurrentRegion.ClearContents ‘ 取込シート を全クリア
Application.ScreenUpdating = False ’ 画面描画 停止
Application.Calculation = xlCalculationManual ‘自動計算停止(手動計算)
‘—–データのある初期フォルダ指定(無くてもOK)———————–
Dim Path As String, WSH As Variant
Set WSH = CreateObject(“WScript.Shell”)
Path = WSH.SpecialFolders(“Desktop”)
Path = Replace(Path, “Desktop”, “Downloads”)
ChDrive “C”
ChDir Path ‘ファイル情報変更前にカレントフォルダを変えると良い
‘—– データのある 初期フォルダの指定完了———————–
’ここから本題のコード
Target = Application.GetOpenFilename(Filefilter:="CSVファイル(*.csv),*.csv")
‘読み込むファイル
Dim strFilePath As String
strFilePath = Target
Dim queryTb As QueryTable
Set queryTb = sh1.QueryTables.add(Connection:=”TEXT;” & strFilePath, _
Destination:=sh1.Range(“A1”)) ‘ CSV を開く
With queryTb
.TextFilePlatform = 932 ‘ 文字コードを指定
.TextFileParseType = xlDelimited ‘ 区切り文字の形式
.TextFileCommaDelimiter = True ‘ カンマ区切り
.TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2) ‘書式 1一般 2文字列 カラムの数だけ書式を設定 20列なら20個2を書く
.RefreshStyle = xlOverwriteCells ‘ セルに上書き
.Refresh ‘ データを表示
.Delete ‘ CSVファイルとの接続を解除
End With
Application.Calculation = xlCalculationAutomatic '自動計算開始
Application.ScreenUpdating = True '画面描画再開
MsgBox ("インポート完了")
End Sub