標題:
請問如何用ARRAY做資料比對?
[打印本頁]
作者:
小俠客
時間:
2011-12-14 15:30
標題:
請問如何用ARRAY做資料比對?
看了一些VBA的教學,看得半懂不懂。還是自己動手寫一些VBA的小程式,抽學得比較快。
最近嘗試用VBA寫BINGO遊戲,造數字卡的部份和抽號碼的部份都能成功,可是用VBA對號碼的部份我卻不了解。
由於BINGO遊戲只要是:直、橫、對角線成一線便可以勝出。以5X5的數字卡為例,是否我需要寫下
5橫、5直、2對角,共12個組合的IF CONDITION才能做比對?
曾問過同事,他建議我可以用ARRAY的方法做,包括抽號碼和造數字卡也可以。
我大概也試過ARRAY,先把隨機數字放在ARRAY中,再順序抽出,但我無法避免重複數字。
所以我想請教大家,小弟的DRAW和PAPER的程序可以用ARRAY改寫嗎?
另外,如何在抽一個新的號碼時比對數字卡,看看是否中獎,謝謝大家。
作者:
GBKEE
時間:
2011-12-16 17:52
本帖最後由 GBKEE 於 2011-12-16 17:53 編輯
回復
1#
小俠客
Option Explicit
Public Rng As Range, Rng1 As Range, Rng2 As Range
Sub Restart()
Sheets(1).Cells.Clear
End Sub
Sub Draw()
Dim B As Integer, C As Integer, d As Integer
With Sheet1
L:
B = Int(100 * Rnd() + 1)
C = Application.CountIf(.Range("A:A"), B) '比對是否重復
If C = 1 Then GoTo L: '重做亂數
d = Application.CountA(.Range("A:A")) + 5
.Cells(d, 1) = B
.Cells(d, 1).Select
Bingo (B)
End With
End Sub
Sub Paper()
Dim E As Range, B As Integer, i As Integer, ii As Integer
Set Rng = [Sheet1!I1:M5] '設定數字區域
For Each E In Rng
L:
B = Int(100 * Rnd() + 1)
C = Application.CountIf(Rng, B) '比對是否重復
If C = 1 Then GoTo L:
E = B
Next
For i = 1 To 5 '設定對角線由左至右區域
If i = 1 Then
Set Rng1 = Rng.Cells(i, i)
Else
Set Rng1 = Union(Rng1, Rng.Cells(i, i))
End If
Next
ii = 5
For i = 1 To 5 '設定對角線由右至左區域
If i = 1 Then
Set Rng2 = Rng.Cells(i, ii)
Else
Set Rng2 = Union(Rng1, Rng.Cells(i, ii))
End If
ii = ii - 1
Next
End Sub
Sub Bingo(No As Integer)
Dim f As Range, d As Integer, C As Range, i As Integer, ii As Integer
Set f = Rng.Find(No, LookIn:=xlValues, LOOKAT:=xlWhole) '尋找數字
If f Is Nothing Then Exit Sub
If Not f Is Nothing Then
f.Font.ColorIndex = 3 '找到數字給字體顏色
f.Font.FontStyle = "粗體" '找到數字給字型樣式
End If
For i = 1 To Rng.Columns.Count
d = 0
For Each C In Rng.Columns(i).Cells
If C.Font.ColorIndex = 3 Then d = d + 1
Next
If d = 5 Then Rng.Columns(i).Select: GoTo ok
Next
For i = 1 To Rng.Rows.Count '檢查橫列
d = 0
For Each C In Rng.Rows(i).Cells
If C.Font.ColorIndex = 3 Then d = d + 1
Next
If d = 5 Then Rng.Rows(i).Select: GoTo ok
Next
d = 0
For Each C In Rng1 '檢查對角線
If C.Font.ColorIndex = 3 Then d = d + 1
Next
If d = 5 Then Rng1.Select: GoTo ok
d = 0
For Each C In Rng2 '檢查對角線
If C.Font.ColorIndex = 3 Then d = d + 1
Next
If d = 5 Then Rng2.Select: GoTo ok
Exit Sub
ok:
MsgBox "Bingo"
End Sub
複製代碼
作者:
Hsieh
時間:
2011-12-16 21:22
回復
1#
小俠客
Public k%
Sub Draw()
If Application.CountA([A:A]) >= k ^ 2 Then Exit Sub
n = Int((k ^ 2) * Rnd + 1)
Do Until IsError(Application.Match(n, [A:A], 0))
n = Int((k ^ 2) * Rnd + 1)
Loop
r = Application.CountA([A:A]) + 1
[A5].Offset(r) = n
Set a = Range("I1").CurrentRegion.Find(n, lookat:=xlWhole)
a.Interior.ColorIndex = 3
Set b = a.EntireColumn
Set c = a.EntireRow
ar = Array(b, c)
For i = 0 To 1
yn = True
For Each x In Intersect(ar(i), Range("I1").CurrentRegion)
If x.Interior.ColorIndex <> 3 Then yn = False: Exit For
Next
If yn = True Then MsgBox "Bango!!!": Exit Sub
Next
Set b = [I1]
Set c = [I1].Offset(, k - 1)
ar = Array(b, c)
For i = 0 To 1
yn = True: x = IIf(i = 0, 1, -1)
For j = 0 To k - 1
If ar(i).Offset(j, j * x).Interior.ColorIndex <> 3 Then yn = False: Exit For
Next
If yn = True Then MsgBox "Bango!!!": Exit Sub
Next
End Sub
Sub Restart()
Sheets(1).Cells.Clear
Sheets(2).Cells.Clear
Sheets(3).Cells.Clear
End Sub
Sub Paper()
k = InputBox("輸入陣列維數", , 5)
ReDim ar(k, k)
ReDim ay(k, k)
For i = 0 To k - 1
For j = 0 To k - 1
ar(i, j) = Rnd
Next
Next
[I1].Resize(k, k) = ar
For i = 0 To k - 1
For j = 0 To k - 1
ay(i, j) = Application.Rank(ar(i, j), [I1].Resize(k, k))
Next
Next
[I1].Resize(k, k) = ay
End Sub
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)