vb 彩牛概率统计for Excel

彩牛统计牛牛概率

主要计算牛一到牛牛和炸弹无牛在excel表中统计列举的牌型概率。代码如下,顺便了解了下vb这门古老的语言。唯一感想就是自动对其功能很强。突然吹使我再温习下代码之美,要把代码写好,写规范啊。路途遥远啊。

顺便附上excel表下载地址:http://download.csdn.net/download/huyonghong2012/10209715

Dim n0, n1, n2, n3, n4, n5, n6, n7, n8, n9, nn, nz As Double
Dim m, n, i, j, r1, r2, r3, r, c, color, zd, cc As Long

Public Function getType(row) As Integer
    c = 0
    zd = 0
    
    For r1 = 1 To 5 Step 1
        For r2 = 2 To 5 Step 1
            For r3 = 3 To 5 Step 1
                If r1 < r2 And r2 < r3 Then
                    If (Cells(row, r1 + 1) + Cells(row, r2 + 1) + Cells(row, r3 + 1)) Mod 10 <> 0 Then
                    Else
                        c = 1
                    End If
                End If
            Next r3
        Next r2
    Next r1
    
    
    If (Cells(row, 2) = Cells(row, 3) And Cells(row, 3) = Cells(row, 4) And Cells(row, 4) = Cells(row, 5)) Or (Cells(row, 2) = Cells(row, 3) And Cells(row, 3) = Cells(row, 4) And Cells(row, 4) = Cells(row, 6)) Or (Cells(row, 2) = Cells(row, 3) And Cells(row, 3) = Cells(row, 5) And Cells(row, 5) = Cells(row, 6)) Or (Cells(row, 2) = Cells(row, 4) And Cells(row, 4) = Cells(row, 5) And Cells(row, 5) = Cells(row, 6)) Or (Cells(row, 3) = Cells(row, 4) And Cells(row, 4) = Cells(row, 5) And Cells(row, 5) = Cells(row, 6)) Then
        zd = 1
    Else
        zd = 0
    End If
    
    
    If zd = 1 Then
        getType = 11
    Else
        If c = 0 Then
            getType = 0
        Else
            If (Cells(row, 2) + Cells(row, 3) + Cells(row, 4) + Cells(row, 5) + Cells(row, 6)) Mod 10 <> 0 Then
                getType = (Cells(row, 2) + Cells(row, 3) + Cells(row, 4) + Cells(row, 5) + Cells(row, 6)) Mod 10
            Else
                getType = 10
            End If
            
        End If
        
    End If
    

End Function

Public Function setColor(row, color)
    For r1 = 1 To 5 Step 1
        Cells(row, r1 + 1).Interior.color = color
        'RGB(255, 255, 255)
    Next r1
End Function

Public Function clear()
     Range("h2") = ""
     Range("h3") = ""
     
     Range("i2") = ""
     Range("i3") = ""

     Range("j2") = ""
     Range("j3") = ""

     n3 = n3 + 1
     Range("k2") = ""
     Range("k3") = ""

     Range("l2") = ""
     Range("l3") = ""

     Range("m2") = ""
     Range("m3") = ""
 
     Range("n2") = ""
     Range("n3") = ""

     Range("o2") = ""
     Range("o3") = ""
 
     Range("p2") = ""
     Range("p3") = ""
     Range("q2") = ""
     Range("q3") = ""
 
     Range("r2") = ""
     Range("r3") = ""
     
 
     Range("s2") = ""
     Range("s3") = ""
     
    Range("g2") = ""
    Range("g3") = ""
End Function

Sub calculate()
    n0 = 0
    n1 = 0
    n2 = 0
    n3 = 0
    n4 = 0
    n5 = 0
    n6 = 0
    n7 = 0
    n8 = 0
    n9 = 0
    nn = 0
    nz = 0
    cc = clear()
    'Range("t3") = (Cells(1, 2) + Cells(1, 3) + Cells(1, 4) + Cells(1, 5) + Cells(1, 6)) Mod 10
    m = Range("t2")
    For j = 1 To m Step 1
        n = getType(j)
        Select Case n
        Case 0
            n0 = n0 + 1
            Range("h2") = n0 / m
            Range("h3") = n0
            color = setColor(j, RGB(0, 0, 255))
        Case 1
            n1 = n1 + 1
            Range("i2") = n1 / m
            Range("i3") = n1
        Case 2
            n2 = n2 + 1
            Range("j2") = n2 / m
            Range("j3") = n2
        Case 3
            n3 = n3 + 1
            Range("k2") = n3 / m
            Range("k3") = n3
        Case 4
            n4 = n4 + 1
            Range("l2") = n4 / m
            Range("l3") = n4
        Case 5
            n5 = n5 + 1
            Range("m2") = n5 / m
            Range("m3") = n5
        Case 6
            n6 = n6 + 1
            Range("n2") = n6 / m
            Range("n3") = n6
        Case 7
            n7 = n7 + 1
            Range("o2") = n7 / m
            Range("o3") = n7
        Case 8
            n8 = n8 + 1
            Range("p2") = n8 / m
            Range("p3") = n8
        Case 9
            n9 = n9 + 1
            Range("q2") = n9 / m
            Range("q3") = n9
        Case 10
            nn = nn + 1
            Range("r2") = nn / m
            Range("r3") = nn
            color = setColor(j, RGB(0, 255, 0))
        Case 11
            nz = nz + 1
            Range("s2") = nz / m
            Range("s3") = nz
            color = setColor(j, RGB(255, 0, 0))
        End Select
        
    Next j
    
    Range("g2") = (n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + nn + nz - 1) / m
    Range("g3") = (n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + nn + nz - 1)

End Sub

你可能感兴趣的:(vb 彩牛概率统计for Excel)