Excel VBA 6 3 3 1 Excel BLOCKGAME.xls Excel 1 OK 2 StepA D B1 B4 C1 C2 StepA StepA Excel Workbook Open StepD BLOCKGAME.xls VBEditor ThisWorkbook 3 1
1 2 2
3 5 UserForm1 4 6 UsorForm2 StepB 3 StepC StepD SetpD StepA Workbook BeforeClose BLOCKGAME_S1.xls UI BLOCKGAME_S1.xls Sheet1 5 Excel 4 3
VBEditor UserForm1 UserFom2 5 6 1 2 Step 1 1 1 BlockGame 2 1 1 2 Public 2 GameEnd False ChallgNo 1 1 Sub BlockGame() Dim i As Integer Dim myrange As Range ' GameEnd = False ChallgNo = 1 B1 ' UserForm1.Show If GameEnd = True Then Exit Sub ' s_blockclear ' B3 s_blockdraw ' s_problem ' B4 B2 Do UserForm2.Show ' Loop Until GameEnd = True ' Range("BlockP").EntireColumn.Hidden = False C1.2 C3 4
UserFom1 2 Initialize 7 7 2 UserForml Private Sub UserForm_Initialize() Dim myspn As MSForms.SpinButton Dim myimg As MSForms.Image Dim i As Integer ' With UserForm2.StartUpPosition = 0.Left = Application.Left + 400.Top = Application.Top + 100 End With For i = 1 To BlockMNo ' Set myspn = UserForm2.Controls("Spinbutton" & i) With myspn.max = ColorNo + 1.Min = 0.Value = 1 End With ' Set myimg = UserForm2.Controls("Image" & i) If ChallgNo = 1 Then myimg.tag = i myimg.backcolor = f_setrgbcolor(i) Else '2 myimg.tag = Cells(8 - i, (ChallgNo - 2) * 2 + 4).Interior.ColorIndex myimg.backcolor = f_setrgbcolor(myimg.tag) ' For i = BlockMNo + 1 To 5 UserForm2.Controls("Spinbutton" & i).visible = False UserForm2.Controls("Image" & i).visible = False 5
UserFom1 Value True Value GameEnd True 3 3 s_blockclear s_blockdraw 1 B3 2 3 Union Union 5 3 '***** Sub s_blockclear() Dim myrange As Range Set myrange = Union(Range("Block5"), Range("BlockP")) With myrange.clear.borders(xledgeleft).linestyle = xlnone.borders(xledgetop).linestyle = xlnone.borders(xledgebottom).linestyle = xlnone.borders(xledgeright).linestyle = xlnone.borders(xlinsidehorizontal).linestyle = xlnone.interior.colorindex = xlnone End With Range("BlockC").ClearContents '***** Sub s_blockdraw() Dim myrange As Range Set myrange = Union(Range("Block" & BlockMNo), _ Range("BlockP").Offset(5 - BlockMNo).Resize(BlockMNo)) With myrange.borders(xledgeleft).linestyle = xlcontinuous.borders(xledgetop).linestyle = xlcontinuous.borders(xledgebottom).linestyle = xlcontinuous.borders(xledgeright).linestyle = xlcontinuous.borders(xlinsidehorizontal).linestyle = xlcontinuous End With 6
s_problem s_problem f_setcolor 4 4 4 Sub s_problem() Dim i As Integer ' Range("BlockP").EntireColumn.Hidden = True ' For i = 1 To BlockMNo Cells(8 - i, 1).Interior.ColorIndex = f_setcolor(i) '***** NO Function f_setcolor(blockno As Integer) Dim i As Integer Dim mycolor As Integer ' Dim myflg As Boolean If BlockNo = 1 Then '1 Randomize f_setcolor = Int(ColorNo * Rnd + 1) Else '2 Do myflg = False Randomize mycolor = Int(ColorNo * Rnd + 1) For i = 1 To BlockNo - 1 If mycolor = Cells(8 - i, 1).Interior.ColorIndex Then myflg = True: Exit For Loop While myflg = True f_setcolor = mycolor End Function 7
UserForm1 UserFom2 5 1 2 3 4 3 5 UserForm2 5 5 UserForm2 Option Explicit Option Base 1 '***** Private Sub UserForm_Initialize() Dim myspn As MSForms.SpinButton Dim myimg As MSForms.Image Dim i As Integer ' With UserForm2.StartUpPosition = 0.Left = Application.Left + 400.Top = Application.Top + 100 End With For i = 1 To BlockMNo ' Set myspn = UserForm2.Controls("Spinbutton" & i) With myspn.max = ColorNo + 1.Min = 0.Value = 1 End With ' Set myimg = UserForm2.Controls("Image" & i) If ChallgNo = 1 Then myimg.tag = i myimg.backcolor = f_setrgbcolor(i) Else '2 myimg.tag = Cells(8 - i, (ChallgNo - 2) * 2 + 4).Interior.ColorIndex myimg.backcolor = f_setrgbcolor(myimg.tag) ' For i = BlockMNo + 1 To 5 UserForm2.Controls("Spinbutton" & i).visible = False UserForm2.Controls("Image" & i).visible = False '***** RGB Function f_setrgbcolor(myidx As Integer) As Long Dim mycol As Long Select Case myidx Case 1: mycol = RGB(0, 0, 0) ' Case 2: mycol = RGB(255, 255, 255) ' Case 3: mycol = RGB(255, 0, 0) ' Case 4: mycol = RGB(0, 255, 0) ' Case 5: mycol = RGB(0, 0, 255) ' Case 6: mycol = RGB(255, 255, 0) ' Case 7: mycol = RGB(255, 0, 255) ' Case 8: mycol = RGB(0, 255, 255) ' End Select f_setrgbcolor = mycol End Function 8
1 5 4 1 6 1 6 1 6 7 Change Change 6 2 5 Colorlndex RGB f_setrgbcolor Tag Change 6 UserForm2 ExcelVBA BLOCKGAME.xls 6 '***** Private Sub SpinButton1_Change() ' Select Case SpinButton1.Value Case 0: SpinButton1.Value = ColorNo Case ColorNo + 1: SpinButton1.Value = 1 End Select ' Image1.BackColor = f_setrgbcolor(spinbutton1.value) Image1.Tag = SpinButton1.Value 9
[ ] [ ] UserFom2 7 7 mycolor 1 mycolor 2 False True 5 UserForm2 OptionBase 1 0 s_check GameEnd True 10
7 '***** Private Sub CommandButton1_Click() Dim myimg As MSForms.Image Dim i As Integer Dim mycolor(8) As Boolean ' For i = 1 To BlockMNo Set myimg = UserForm2.Controls("Image" & i) If mycolor(myimg.tag) = True Then MsgBox " ": Exit Sub Else Cells(8 - i, (ChallgNo - 1) * 2 + 4).Interior.ColorIndex = myimg.tag mycolor(myimg.tag) = True s_check Unload Me '***** Private Sub CommandButton2_Click() GameEnd = True ' Unload Me '***** Sub s_check() Dim i As Integer, j As Integer ' For i = 1 To BlockMNo For j = 1 To BlockMNo If Cells(8 - i, 1).Interior.ColorIndex = _ Cells(8 - j, (ChallgNo - 1) * 2 + 4).Interior.ColorIndex Then If i = j Then Cells(9, (ChallgNo - 1) * 2 + 4).Value = Cells(9, (ChallgNo - 1) * 2 + 4).Value + 1 Else Cells(10, (ChallgNo - 1) * 2 + 4).Value = Cells(10, (ChallgNo - 1) * 2 + 4).Value + 1 Exit For ' If Cells(9, (ChallgNo - 1) * 2 + 4).Value = BlockMNo Then MsgBox " " GameEnd = True ElseIf ChallgNo = 8 Then MsgBox " " GameEnd = True ChallgNo = ChallgNo + 1 ' 11
6 3 20 3 6 20 6 120 6720 12
13