chatGPTで適当に自分用に貼り付け用

おもにエクセルのマクロ

以下は、修正後のプログラムです。このプログラムでは、`shiftCount`を変更してシフト数を調整することで、1ヶ月の出勤日数を18日になるように設定します。

```vba
Sub CreateShift()
Dim staff As Variant
Dim attendance As Variant
Dim numOfStaff As Integer
Dim shiftCount As Integer
Dim daysPerMonth As Integer
Dim i As Integer
Dim j As Integer

'スタッフの名前
staff = Array("スタッフ1", "スタッフ2", "スタッフ3", "スタッフ4", "スタッフ5", "スタッフ6")

'土日祝の出勤回数
attendance = Array(0, 0, 0, 0, 0, 0)

numOfStaff = UBound(staff) + 1
daysPerMonth = 18
shiftCount = daysPerMonth \/ 2 '1日2人出勤なのでシフト数は出勤日数の半分

'2人のスタッフをランダムに選出し、シフトに設定
For i = 1 To shiftCount
For j = 1 To 2
Dim randomIndex As Integer
Dim selectedStaff As String

Do
randomIndex = Int((numOfStaff - 1 + 1) * Rnd + 1)
selectedStaff = staff(randomIndex - 1)
Loop Until attendance(randomIndex - 1) < shiftCount

attendance(randomIndex - 1) = attendance(randomIndex - 1) + 1
Cells(i, j).Value = selectedStaff
Next j
Next i

'土日祝の出勤回数が全員同じ数になるように調整
Do
Dim minAttendance As Integer
Dim minIndex As Integer
Dim maxAttendance As Integer
Dim maxIndex As Integer

'最も出勤回数の少ないスタッフと最も出勤回数の多いスタッフを探す
minAttendance = Application.Min(attendance)
minIndex = Application.Match(minAttendance, attendance, 0)
maxAttendance = Application.Max(attendance)
maxIndex = Application.Match(maxAttendance, attendance, 0)

'出勤回数の多いスタッフのシフトを変更して出勤回数を調整
For i = 1 To shiftCount
If Cells(i, 1).Value = staff(maxIndex - 1) Or Cells(i, 2).Value = staff(maxIndex - 1) Then
attendance(minIndex - 1) = attendance(minIndex - 1) + 1
attendance(maxIndex - 1) = attendance(maxIndex - 1) - 1
Cells(i, 1).Value = staff(minIndex - 1)
Exit For
End If
Next i
Loop Until Application.Min(attendance) = shiftCount

'結果を出力
For i = 1 To shiftCount
For j = 1 To 2
Debug.Print Cells(i, j).Value
Next j
Next i
End Sub
```

このプログラムを実行すると、VBAエディタ内で結果がデバッグウィンドウに表示されます。必要に応じてデバッグウィンドウの代わりに結果をExcelシートに出力する場合は、`Debug.Print Cells(i, j).Value` を `Cells(i, j).Value = Cells(i, j).Value` に変更してください。

この例では、1ヶ月の出勤日数を18日に設定し、その日数に基づいてシフトを作成します。ただし、スタッフ数が6人、出勤日数が18日、1日2人出勤するためシフト数が9回と固定されており、必要に応じて変更する必要があります。