基本上我试图循环遍历数组中的所有值,并使用Inputbox计算大于用户指定值的值的数量,并尝试使用IF语句来确保1和1之间的数字。输入100。完成之后,我只想在之后的消息框中显示结果。
这是我到目前为止所做的:
Dim arr As Variant
arr = Range("A1:J10")
Range("A1").Resize(UBound(arr, 1), UBound(arr, 2)) = arr
Dim val As String
val = InputBox("Enter an integer value")
If val < 1 Or val > 100 Then
' tells the user to try again
MsgBox "You did not enter a value from 1 to 100 , try again"
val = Inputbox("Enter an integer value")
Else
End If
基本上使用if语句来验证用户输入的内容并循环遍历数组。
答案 0 :(得分:1)
我建议如下,使用内置的CountIF
函数将所有循环和数组放在一起。
Do
Dim val As Variant
val = InputBox("Enter an integer value")
If IsNumeric(val) And val > 1 And val < 100 Then
Dim bPass As Boolean
bPass = True
End If
If Not bPass Then MsgBox "You did not enter a value from 1 to 100 , try again"
Loop Until bPass
Dim lCountIf As Long
lCountIf = WorksheetFunction.CountIf(Range("A1:J10"), ">" & val)
MsgBox lCountIf & " values greater than " & val & "in Range."
答案 1 :(得分:1)
我&#39; d
使用Application.InputBox()来强制输入数字
避免使用数组并使用WorksheetFunction.CountIf()
如下:
Dim val As Integer
Do
val = CInt(Application.InputBox(prompt:="Enter an integer value between 1 and 100", Type:=1))
Loop while val <1 And val > 100
MsgBox WorksheetFunction.CountIf(Range("A1:J10"), ">" & val)
答案 2 :(得分:0)
Dim val As String
val = InputBox("Enter an integer value")
Do While val < 1 Or val > 100
' tells the user to try again
MsgBox "You did not enter a value from 1 to 100 , try again"
val = InputBox("Enter an integer value")
Loop
'checks array if the number is greater then input number
Dim MyRange As Range
Dim cell As Range
Dim counter as integer
counter = 0
Set MyRange = Range("A1:J10")
For Each cell In MyRange
If cell.Value > CInt(val) Then
counter = counter + 1
cell.Interior.Color = RGB(255, 0, 0) 'or cell.Interior.ColorIndex = 3
End If
Next
msgbox "Total number greater then input number is: " & counter