locked
Hard to explain... Average calculation difficulty. RRS feed

  • Question

  • I have a dart score keeper which mostly has labels, buttons and one textbox to enter scores. It's my first big project using VBA. I'm having big problems just trying to keep a simple average.

    The problem arises from the fact that with the way I have coded it, the average calculation seems to be a step behind which is why I get divides by zero errors. Because at the start of the game, the thrown (darts thrown) Integer displayed in dartsThrown.caption is at zero... then when I click "scoreinput_click" it adds 3 to it but also then runs the average calculation based on the thrown integer, but it's always going to be 3 behind with the way I have it here.

    Here is the code from two Sub Routines... I have bolded the main players in the average calculation.

    Dim score As Integer Dim scored As Integer Dim total As Long Dim thrown As Integer Dim used As Integer Dim avg As Integer Private Sub scoreInput_Click() thrown = dartsThrown.Caption score = Label1.Caption scored = TextBox1 total = total + scored Select Case scored Case Is < 0, 163, 166, 169, 172, 173, 175, 177, 178, 179, Is >= 181 MsgBox "Invalid Score!" Exit Sub End Select Select Case score Case Is < 0 'Is = 1 Why doesnt this work? MsgBox "You are Bust!" Exit Sub End Select Select Case scored Case 60 To 99 sixtyplus.Caption = sixtyplus + 1 Case 100 To 139 tonPlus.Caption = tonPlus + 1 Case 140 To 179 t40.Caption = t40 + 1 Case 180 t80.Caption = t80 + 1 End Select dartsThrown.Caption = thrown + 3 Label1.Caption = (score - scored) avg = total / thrown * 3 TextBox1.Text = "" TextBox1.Activate If Label1.Caption = 0 Then Call Store_Click End If Call CalcAvg_Click End Sub Private Sub CalcAvg_Click() Average.Caption = avg

    End Sub


     

    Monday, April 8, 2013 12:10 AM

Answers

  • Change the line

        dartsThrown.Caption = thrown + 3

    to

        thrown = thrown + 3
        dartsThrown.Caption = thrown


    Regards, Hans Vogelaar

    • Marked as answer by KennyJC Monday, April 8, 2013 10:59 PM
    Monday, April 8, 2013 5:59 AM