実現したいこと
いろいろな条件を満たした掃除当番決めをするコードをつくりたいです。
Geminiに完全お任せで作成してもらいました。
(Geminiに提示した内容を補足に添付しています)
発生している問題・分からないこと
作成されたコードをコピペで実行しようとしたところエラーが出ました。
Excelの画面上にプルダウンと実行ボタンは作成されていますが、年月を選択して実行ボタンを押すとエラーが表示されます。
エラーメッセージ
error
1コンパイルエラー:引数は省略できません。
該当のソースコード
Sub CreateCleaningSchedule() ' 変数宣言 Dim GroupA As Variant, GroupB As Variant Dim BathDays As Variant, LaundryDays As Variant Dim Schedule As Object Dim YearMonth As Date, StartDate As Date, EndDate As Date ' YearMonthの型を変更 Dim CurrentDate As Date Dim i As Long, j As Long, k As Long Dim Pair As Variant, Place As String ' グループメンバーと掃除場所の定義 GroupA = Array("a", "b", "c", "d", "e", "f") GroupB = Array("g", "h", "i", "j", "k", "l", "m") BathDays = Array(vbMonday, vbThursday) ' お風呂・シャワー掃除の曜日 LaundryDays = Array(vbWednesday) ' 洗濯室・補食室掃除の曜日 ' 年月を取得(プルダウンから取得する想定) YearMonth = CDate(Sheets("Sheet1").Range("A1").Value) ' CDateで日付型に変換 StartDate = DateSerial(Year(YearMonth), Month(YearMonth), 1) EndDate = DateSerial(Year(YearMonth), Month(YearMonth) + 1, 0) ' スケジュールを格納するDictionaryオブジェクトを作成 Set Schedule = CreateObject("Scripting.Dictionary") ' 日付ごとに担当者を割り当てる CurrentDate = StartDate Do While CurrentDate <= EndDate If IsDateInArray(Weekday(CurrentDate, vbMonday), BathDays) Then ' お風呂・シャワー掃除 Pair = GetRandomPair(GroupA, GroupB, Schedule) Place = "お風呂・シャワー" Schedule(CurrentDate) = Pair & " - " & Place ElseIf IsDateInArray(Weekday(CurrentDate, vbMonday), LaundryDays) Then ' 洗濯室・補食室掃除 Pair = GetRandomPair(GroupA, GroupB, Schedule) Place = "洗濯室・補食室" Schedule(CurrentDate) = Pair & " - " & Place End If CurrentDate = CurrentDate + 1 Loop ' スケジュールをExcelシートに出力 OutputScheduleToSheet Schedule, StartDate, EndDate MsgBox "掃除当番表を作成しました。" End Sub ' ランダムなペアを取得する関数 Function GetRandomPair(GroupA As Variant, GroupB As Variant, Schedule As Object, AssignedPairs As Object) As Variant Randamize Dim Pair As Variant Dim MemberA As String, MemberB As String Do MemberA = GroupA(Int(Rnd() * UBound(GroupA))) MemberB = GroupB(Int(Rnd() * UBound(GroupB))) Pair = Array(MemberA, MemberB) Loop While AssignedPairs.Exists(MemberA & "," & MemberB) Or AssignedPairs.Exists(MemberB & "," & MemberA) AssignedPairs.Add MemberA & "," & MemberB, True GetRandomPair = Pair End Function ' ペアが既に割り当てられているか確認する関数 'Function IsPairAlreadyAssigned(Pair As Variant, Schedule As Object) As Boolean 'End Function ' 日付が曜日配列に含まれるか確認する関数 Function IsDateInArray(DateValue As Long, DateArray As Variant) As Boolean Dim i As Long For i = LBound(DateArray) To UBound(DateArray) If DateValue = DateArray(i) Then IsDateInArray = True Exit Function End If Next i IsDateInArray = False End Function ' スケジュールをExcelシートに出力する関数 Sub OutputScheduleToSheet(Schedule As Object, StartDate As Date, EndDate As Date) Dim Key As Variant, Item As Variant Dim RowNum As Long RowNum = 2 ' 出力開始行 ' カレンダー形式で出力(簡略化のため、日付と担当者のみ出力) For Each Key In Schedule.Keys If Key >= StartDate And Key <= EndDate Then Sheets("Sheet1").Cells(RowNum, 1).Value = Key Sheets("Sheet1").Cells(RowNum, 2).Value = Join(Split(Schedule(Key), " - "), ",") RowNum = RowNum + 1 End If Next Key End Sub
Sub CreateYearMonthDropdown() Dim YearMonthList As Variant Dim i As Long ' 年月リストを作成 ReDim YearMonthList(0 To 11) For i = 0 To 11 YearMonthList(i) = Format(DateSerial(2025, 4 + i, 1), "yyyy/m") Next i ' プルダウンを作成 With Sheets("Sheet1").Range("A1").Validation .Delete .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=Join(YearMonthList, ",") .InCellDropdown = True End With ' 実行ボタンを作成 With Sheets("Sheet1").Buttons.Add(100, 10, 80, 20) ' ボタンの位置とサイズ .OnAction = "CreateCleaningSchedule" .Caption = "実行" End With End Sub
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
Geminiにエラー内容を送信し、デバックをさせたが同じことの繰り返しになってしまいました
新しいコードを追加してください(エラー変わらず)➝追加したコードが不要です(エラー変わらず)➝新しい…
補足
本人はほとんど知識がない学生です。
寮で係を押し付けられたためGeminiと皆さんに頼っています。
追記:使い方を間違えていたようで、ご不快な思いをした方は申し訳ございません。
コメントでご指摘いただいた内容を追記します
黄色くなっていたのは
' お風呂・シャワー掃除
Pair = GetRandomPair(GroupA, GroupB, Schedule)
の GetRandomPairという部分です
掃除当番を決めるコード
・毎週月曜と木曜にお風呂とシャワーの掃除をする
・毎週水曜に洗濯室と補食室の掃除をする
・4か所それぞれの場所を2人で掃除する
・一緒に掃除をするペアは毎回異なるようにする
・以下のグループAとグループBから1人ずつでペアを組む
グループA、6人
グループB、7人
・ペアに偏りがないようにする
・それぞれの場所を掃除する回数に偏りがないようにする
・2025年4月から2026年3月までの当番表を一気に作成
・エクセルの画面上に、年月を選択できるプルダウンを作成
・年月を選択して実行ボタンを押すと選択した年月の部分が表示されるようにする
・選択できる年月は"2025.4"から"2026.3"とする
・カレンダーの中にその日の当番2人と場所が表示されるようにする

回答1件
あなたの回答
tips
プレビュー