协助debuggingvba模块

我想知道是否有人可以帮助我debugging下面的模块,我相信我唯一的问题在于最后一个(if)语句为了单元格填充颜色。 通用模块创build一个随机数的单元格范围,如果在input框中input了某个特定值的input,则该范围应该用红色填充。 谢谢!

Option Explicit Sub test() Dim i As Integer Dim j As Integer Dim user As Integer Dim cell As Range Dim random_number As Integer Dim itm As Integer For Each cell In Range("A1:J10") random_number = Int(100 * Rnd) + 1 cell.Value = random_number Next Dim mycount As Integer Dim myarray(1 To 10, 1 To 10) For i = 1 To 10 For j = 1 To 10 myarray(i, j) = Cells(i, j).Value Next Next user = CInt(InputBox("enter a number betweeen 1 and 100")) If user < 1 Or user > 100 Then msgbox ("Error, wrong value entered") Exit Sub End If For i = 1 To 10 For j = 1 To 10 If myarray(i, j) > user Then Range(Cells(i, j)).Interior.Color = RGB(255, 0, 0) itm = itm + 1 End If Next Next msgbox itm End Sub 

请参阅@ Gary的学生评论来debugging您的代码。

如果你愿意学习使用arrays,范围和Excel / VBA二重奏的强大function,如果你要长期使用这种技术,这是必须的 ,请尝试用这种方法重写你的代码:

 Sub test() Dim user As Long With Worksheets("Sheet1").Range("A1:J10") ' Generate random numbers using formula then fix values .Formula = "=RANDBETWEEN(1, 100)" .value = .value ' Enter a number user = Application.InputBox("enter a number betweeen 1 and 100", Type:=1) ' <-- Enter a number If user < 1 Or user > 100 Then MsgBox ("Error, wrong value entered") Exit Sub End If ' Higlight the value using conditional formatting. .FormatConditions.Delete .FormatConditions.Add(xlCellValue, xlGreater, user).Interior.Color = RGB(255, 0, 0) ' Count of items that match the value MsgBox Application.CountIf(.Cells, ">" & user) End With End Sub