tintin
01-04-2007, 11:33 AM
Hi all,
Am pretty new to VBA programming so am teaching myself by writing different types of programs. My first major program is to calculate the proportion of heads that shows up in n (any integer) toss of a coin. I tried doing this by asking the user to enter the amount of tosses of the coin their desire in one cell, which is then passed to the macro to calculate, and return to another cell in the same sheet, the percentage of times heads shows up. The only problem, is that the macro is not calculating and outputting the numbers automatically as I enter them into the cell even though am using sheetchange.
Private Sub workbook_sheetchange(ByVal Target As Range)
If Target.Worksheets("Coin Toss").Range("D5") Then
Call headtossprob
End If
End Sub
Sub headtossprob()
Dim ntoss, heads, i As Variant
Dim prob, hproportion As Double
ntoss = Worksheets("Coin Toss").Range("D5")
head = 0
Randomize
For i = 0 To ntoss
prob = Rnd
If prob > 0.5 Then
heads = heads + 1
End If
Next i
hproportion = heads / ntoss
Worksheets("Coin Toss").Range("D8") = hproportion
End Sub
Am pretty new to VBA programming so am teaching myself by writing different types of programs. My first major program is to calculate the proportion of heads that shows up in n (any integer) toss of a coin. I tried doing this by asking the user to enter the amount of tosses of the coin their desire in one cell, which is then passed to the macro to calculate, and return to another cell in the same sheet, the percentage of times heads shows up. The only problem, is that the macro is not calculating and outputting the numbers automatically as I enter them into the cell even though am using sheetchange.
Private Sub workbook_sheetchange(ByVal Target As Range)
If Target.Worksheets("Coin Toss").Range("D5") Then
Call headtossprob
End If
End Sub
Sub headtossprob()
Dim ntoss, heads, i As Variant
Dim prob, hproportion As Double
ntoss = Worksheets("Coin Toss").Range("D5")
head = 0
Randomize
For i = 0 To ntoss
prob = Rnd
If prob > 0.5 Then
heads = heads + 1
End If
Next i
hproportion = heads / ntoss
Worksheets("Coin Toss").Range("D8") = hproportion
End Sub