我目前正在尝试编写一个脚本,该脚本将从一顿饭菜中随机抽取一顿饭并将其输入日历中。它产生4周(周日至周六),每周有两顿饭。我想让它每次从列表中随机抽取一餐时都对照它已经抽取的餐时进行检查,以防止同一餐在一个月内出现两次。以下是我写的内容:
Dim ranDnr As Integer
Dim ranDnr2 As Integer
Dim usdMls(3) As Integer
Dim usdMls2(3) As Integer
Sub GnrtMlsMnthClndr()
Randomize
For i = 0 To 3
For j = 0 To 3
For x = 2 To 17 Step 5
For y = 2 To 17 Step 5
ranDnr = Int(60 * Rnd + 1)
While IsInArray(ByVal ranDnr, ByVal usdMls) = True
ranDnr = Int(60 * Rnd + 1)
Wend
usdMls(i) = ranDnr
Worksheets("Sheet1").Cells(x, 1) = Worksheets("Sheet2").Cells(ranDnr, 1)
ranDnr2 = Int(60 * Rnd + 1)
While IsInArray2(ByVal ranDnr2, ByVal usdMls2) = True
ranDnr2 = Int(60 * Rnd + 1)
Wend
usdMls2(j) = ranDnr2
Worksheets("Sheet1").Cells(x, 9) = Worksheets("Sheet2").Cells(ranDnr2, 1)
Next y
Next x
Next j
Next i
End Sub
Public Function IsInArray(ByVal ranDnr, ByVal usdMls) As Boolean
'Dim i
For i = 0 To 3
If usdMls(i) = ranDnr Then
IsInArray = True
Exit Function
End If
Next i
IsInArray = False
End Function
Public Function IsInArray2(ByVal ranDnr2, ByVal usdMls2) As Boolean
'Dim i
For i = 0 To 3
If usdMls2(i) = ranDnr2 Then
IsInArray2 = True
Exit Function
End If
Next i
IsInArray2 = False
End Function
我真的对VBA缺乏经验,完全不知道为什么它不起作用,因为对我来说这似乎合乎逻辑,并且在我运行它时不会产生任何错误。可以提供的任何帮助将不胜感激。