これは私の最初の重要な VBA プログラムであり、BASIC のさまざまな方言で以前に配列を使用したことがあり、過去 3、4 晩、Walkenbach の「Excel VBA for Dummies」を熟読して過ごしましたが、当惑しています。
配列の 1 つの要素の値が 1 増加したことを期待していましたが、475 回の実行のうち、コードが期待どおりに実行されたのはわずか 449 回でした。他の 26 回の実行では、代わりに 2 増加した 1 つの要素が生成されました。さらに奇妙なことに、この最初のコード ブロックによってアクセスされる 12 の要素のうち、この動作を示すのはそのうちの 4 つだけであり、他の要素は期待どおりに 1 を返します。
誤った関数が使用する 2 つのテーブルを投稿することができれば役立つ場合がありますが、データが何であれ、これがどのように発生するかわかりません。
'UDF to roll a number of Dice of specified size and total them
Function Dice(NoOfDice, DiceSize)
Dice = 0
For i = 1 To NoOfDice
Dice = Dice + WorksheetFunction.RandBetween(1, DiceSize)
Next
End Function
'UDF to generate a skill from the MOS Table
Function MOSSkill(ArmOfService, TechLevel)
Roll = Dice(1, 6)
If TechLevel = 2 Then Roll = Roll + 1 'TL-11 or less = 1; TL-12+ = 2
MOSSkill = WorksheetFunction.VLookup((WorksheetFunction.VLookup(Roll, (Range(Cells(4, 2), Cells(10, 8))), ArmOfService, False)), (Range(Cells(9, 32), Cells(39, 33))), 2, False)
End Function
Sub MercOne()
Randomize
Dim Merc(86)
45 For i = 1 To 86
Merc(i) = 0
Next
TechLevel = Dice(1, 2)
Roll = Dice(2, 6)
If Merc(2) >= 6 Then Roll = Roll + 1
If Merc(3) >= 5 Then Roll = Roll + 2
If Roll < 5 Then GoTo 45
ArmOfService = Dice(1, 4)
If ArmOfService = 4 Then
ArmOfService = ArmOfService + 2
Else: ArmOfService = ArmOfService + 1
End If
'Array Check 1
For i = 7 To 37
Debug.Print Merc(i); ",";
Next
Debug.Print
Merc(MOSSkill(ArmOfService, TechLevel)) = Merc(MOSSkill(ArmOfService, TechLevel)) + 1
'Array Check 2
For i = 7 To 37
Debug.Print Merc(i); ",";
Next
Debug.Print
End Sub