フォルダにある画像ファイルを片っ端から貼り付けたいと思ったので、chatGPTに聞いてみた。今度試してみます。
' 必要な参照を追加する ' Microsoft Scripting Runtimeを追加する必要があります ' Tools > References > Microsoft Scripting Runtime ' 新しいシートを作成する Dim ws As Worksheet Set ws = ThisWorkbook.Sheets.Add ws.Name = "Images" ' フォルダからすべての画像ファイルを取得する Function GetImageFiles(folderPath As String) As Collection Dim fso As New FileSystemObject Dim files As Collection Set files = New Collection Dim file As File For Each file In fso.GetFolder(folderPath).Files If file.Type Like "image/*" Then files.Add file.Path End If Next Set GetImageFiles = files End Function ' フォルダ内のすべての画像をシートに貼り付ける Sub InsertImages() ' 画像を挿入する開始位置 Dim startCell As Range Set startCell = ThisWorkbook.Sheets("Images").Cells(1, 1) ' 画像が含まれるフォルダのパス Dim folderPath As String folderPath = "C:\images" ' 画像ファイルのコレクションを取得する Dim files As Collection Set files = GetImageFiles(folderPath) ' 画像をシートに挿入する Dim file As Variant For Each file In files Dim pic As Picture Set pic = ThisWorkbook.Sheets("Images").Pictures.Insert(file) pic.Top = startCell.Top pic.Left = startCell.Left ' 次のセルに移動する Set startCell = startCell.Offset(1, 0) Next End Sub