エクセルマクロで、日付別にCSVを分けて作成するVBAコードをご紹介します。
このページのマクロコードは、コピペで使えます。
ぜひお試しください!
日付別にCSVを分けて出力
以下のマクロを実行すると、CSVを日付別に出力します。
Sub 日付別のCSVを作成()
Dim MyPath As String
Dim myDic As Object
Dim myKey As Variant
Dim myList As Variant
Dim i As Long
Dim Q As Long
Dim Paste_Cnt As Long
Paste_Cnt = 1
MyPath = ActiveWorkbook.Path
Set myDic = CreateObject("Scripting.Dictionary")
'A列~C列のデータを配列に格納
myList = Range("A2", Range("A" & Rows.Count). _
End(xlUp)).Resize(, 3).Value
'重複しない日付を取得
For i = 1 To UBound(myList, 1)
If Not myDic.exists(myList(i, 2)) Then
myDic.Add Key:=myList(i, 2), Item:=myList(i, 2)
End If
Next
'重複しない日付を格納
myKey = myDic.keys
'重複しない日付の分をループ
For i = 0 To UBound(myKey)
'新規ファイル作成
Workbooks.Add
'新規ファイルにデータ転記
For Q = 1 To UBound(myList)
If myList(Q, 2) = myKey(i) Then
With ActiveSheet
.Cells(Paste_Cnt, 1) = myList(Q, 1)
.Cells(Paste_Cnt, 2) = Format(myList(Q, 2), "yyyy/mm/dd")
.Cells(Paste_Cnt, 2).NumberFormatLocal = "yyyy/mm/dd"
.Cells(Paste_Cnt, 3) = myList(Q, 3)
End With
Paste_Cnt = Paste_Cnt + 1
End If
Next Q
Paste_Cnt = 1
'CSV作成
ActiveWorkbook.SaveAs Filename:= _
MyPath & "\" & Format(myKey(i), "yyyymmdd"), FileFormat:=xlCSV, Local:=True
'CSVを閉じる
Application.DisplayAlerts = False
ActiveWorkbook.Close True
Application.DisplayAlerts = True
Next i
'開放
Set myDic = Nothing
End Sub
マクロ実行後
日付別にCSVファイルを出力しました。
CSVファイルを作成した際に、日付の表示形式が逆になってしまう現象が発生しました。
以下コードを追加して表示形式を指定することで解決しました!
(上でご紹介しているコードに含まれています。)
[.Cells(Paste_Cnt, 2).NumberFormatLocal = “yyyy/mm/dd”]
ご覧いただき、ありがとうございます!

【エクセルマクロ】複数セルの重複しないリストを作成する
...