【Excel VBA應用】計算骰子的平均點數及全距

上一篇介紹了如何丟出五顆骰子,這次延續主題,教大家如何計算五顆骰子的平均值及全距,並寫入預定的儲存格內。
Excel VBA語法如下:
‘設定迴圈次數,可以隨喜好設定上限值
For i = 1 To TextBox8.Value
        
        ‘隨機骰出五顆骰子的點數
        TextBox1.Value = Int((ComboBox1.Value – ComboBox6.Value + 1) * Rnd + ComboBox6.Value)
        TextBox2.Value = Int((ComboBox2.Value – ComboBox7.Value + 1) * Rnd + ComboBox7.Value)
        TextBox3.Value = Int((ComboBox3.Value – ComboBox8.Value + 1) * Rnd + ComboBox8.Value)
        TextBox4.Value = Int((ComboBox4.Value – ComboBox9.Value + 1) * Rnd + ComboBox9.Value)
        TextBox5.Value = Int((ComboBox5.Value – ComboBox10.Value + 1) * Rnd + ComboBox10.Value)
        
        ‘計算五顆骰子的平均點數
        TextBox6.Value = WorksheetFunction.Average(TextBox1.Value, TextBox2.Value, TextBox3.Value, TextBox4.Value, TextBox5.Value)
    
        ActiveCell.Offset(0, 1).Select
        ‘寫入五顆骰子的平均點數
        ActiveCell.Offset(0, 1).Value = TextBox6.Value
        ‘寫入五顆骰子的點數
        ActiveCell.Offset(1, 1).Value = TextBox1.Value
        ActiveCell.Offset(2, 1).Value = TextBox2.Value
        ActiveCell.Offset(3, 1).Value = TextBox3.Value
        ActiveCell.Offset(4, 1).Value = TextBox4.Value
        ActiveCell.Offset(5, 1).Value = TextBox5.Value
        ‘寫入五顆骰子點數的全距
        ActiveCell.Offset(6, 1).Value = WorksheetFunction.Max(TextBox1.Value, TextBox2.Value, TextBox3.Value, TextBox4.Value, TextBox5.Value) – WorksheetFunction.Min(TextBox1.Value, TextBox2.Value, TextBox3.Value, TextBox4.Value, TextBox5.Value)
        
    Next
相關文章:
瀏覽次數: 66

1 則留言

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

Interactive Control Chart