|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub test()
- Dim myarr(1 To 10, 1 To 10) As Integer
- Range("A1", "J10").Interior.Color = xlNone
- For i = 1 To 10
- For j = 1 To 10
- myarr(i, j) = (i - 1) * 10 + j
- Next
- Next
- [a1].Resize(10, 10) = myarr
-
- Dim rndNum As Integer
- rndNum = WorksheetFunction.RandBetween(1, 100)
-
- Dim greenF, redF, yellowF
- greenF = False '猜对标记绿色
- redF = False '涂红标记,代表猜大
- yellowF = False '涂黄标记,代表猜小
- Dim myGuess As Integer
- Dim myRow As Integer, myCol As Integer
- Dim oldRedRow As Integer, oldRedCol As Integer
- Dim oldYellowRow As Integer, oldYellowCol As Integer
-
- i = 0
- Do
- myGuess = CInt(InputBox("输入你猜的数(1-100):"))
- i = i + 1
- myRow = (myGuess - 1) \ 10 + 1
- myCol = myGuess Mod 10: If myCol = 0 Then myCol = 10
-
- If myGuess > rndNum Then
- If redF Then Cells(oldRedRow, oldRedCol).Interior.Color = xlNone
- Cells(myRow, myCol).Interior.Color = RGB(255, 0, 0)
- redF = True '红标记
- oldRedRow = myRow: oldRedCol = myCol
-
- MsgBox "你猜的大了"
-
- ElseIf myGuess < rndNum Then
- If yellowF Then Cells(oldYellowRow, oldYellowCol).Interior.Color = xlNone
- Cells(myRow, myCol).Interior.Color = RGB(255, 255, 0)
- yellowF = True '黄标记
- oldYellowRow = myRow: oldYellowCol = myCol
- MsgBox "你猜的小了"
-
- Else
- Cells(myRow, myCol).Interior.Color = RGB(0, 255, 0)
- greenF = True '绿标记
- MsgBox "猜对了," & "这个数是" & rndNum & ",你猜了" & i & "次"
- End If
-
- Loop Until greenF
- End Sub
复制代码
|
|