Sub test()
Dim n As Integer, i As Integer, arr(7), sum1 As Integer, sum2 As Integer
n1 = Application.WorksheetFunction.RandBetween(1, 8)
Do
n2 = Application.WorksheetFunction.RandBetween(1, 8)
Loop While n2 = n1
Do
n3 = Application.WorksheetFunction.RandBetween(1, 8)
Loop While n3 = n2 Or n3 = n1
Do
n4 = Application.WorksheetFunction.RandBetween(1, 8)
Loop While n4 = n3 Or n4 = n2 Or n4 = n1
sum1 = Cells(1, n1) + Cells(1, n2) + Cells(1, n3) + Cells(1, n4)
sum2 = Application.WorksheetFunction.Sum(Range("a1:h1")) - sum1
Range("a1:h1").Interior.ColorIndex = 2
Cells(1, n1).Interior.ColorIndex = 43
Cells(1, n2).Interior.ColorIndex = 43
Cells(1, n3).Interior.ColorIndex = 43
Cells(1, n4).Interior.ColorIndex = 43
mod1 = sum1 Mod 10
MsgBox "mod1=" & mod1
mod2 = sum2 Mod 10
MsgBox "mod2=" & mod2
If mod1 = mod2 Then
Range("i1").Value = "TRUE"
Else
Range("i1") = "FALSE"
End If
MsgBox "余数相等事件为:" & Range("i1").Value
End Sub
若把代码中msgbox开头的行删除可去除弹出窗口,把=43的那4行删除可去除背景颜色突出。
初学VBA,写得很不规范,还请各位知友批评指正!楼主还有什么不明白的,亦可追问。