如果然后在范围内vba

我试图让这个VBA代码来确定一个范围之间的值,然后如果条件符合,然后颜色单元格,但是我无法得到如果然后语句正确。

Option Explicit Sub TestRange() Dim Str, lst, y, Value1, Value2 Dim Rng As Range Sheets("Test").Activate Str = Sheets("Test").Range("A2").Address lst = Sheets("Test").Range("A2").Cells.SpecialCells(xlCellTypeLastCell).Address Sheets("Test").Range(Str & ":" & lst).Select With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorDark1 .TintAndShade = -4.99893185216834E-02 .PatternTintAndShade = 0 End With here: Value1 = InputBox("Please enter the lowest score in your range", "CS2") Value2 = InputBox("Please enter the highest score in your range", "CS2") If Value2 < Value1 Then MsgBox "Your Second Value is smaller than your first value" & vbNewLine & _ "Please submit a value higher than your first value", vbExclamation GoTo here End If Set Rng = Sheets("Test").Range(Str & ":" & lst) For Each y In Rng If y >= Value1 And y <= Value2 Then y.Select With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent2 .TintAndShade = 0.799981688894314 .PatternTintAndShade = 0 End With End If Next y End Sub 

InputBox返回一个String ,你永远不会将返回值转换为数字types。 这意味着你正在执行string比较 ,而不是数字比较。 如果其中一个string长于另一个string,它只会根据字符代码比较较短string中的字符数:

 Private Sub Example() Debug.Print "10" > "5" 'This returns false. End Sub 

您首先需要validation用户在InputBox内容实际上是一个数字,然后将其转换为数字types, 然后执行比较。 我也摆脱了Goto和构造input序列的方式,用户不必重新input有效的值:

 Dim userInput As String Dim firstValue As Long Dim secondValue As Long Dim validInput As Boolean Do userInput = InputBox("Please enter the lowest score in your range", "CS2") If IsNumeric(userInput) Then firstValue = CLng(userInput) validInput = True Else MsgBox "Lowest score must be a number." End If Loop While Not validInput Do validInput = False userInput = InputBox("Please enter the highest score in your range", "CS2") If IsNumeric(userInput) Then secondValue = CLng(userInput) If secondValue > firstValue Then validInput = True Else MsgBox "Your Second Value is smaller than your first value" & vbNewLine & _ "Please submit a value higher than your first value", vbExclamation End If Else MsgBox "Highest score must be a number." End If Loop While Not validInput 

请注意,需要进行额外的testing以避免溢出错误。 如果你需要一个浮点数,你可以使用CCurCDbl

有一个Range.Value2属性 。 尽量不要重新使用保留字,特别是当有不明确的方法时。

Excel应用程序InputBox方法允许您专门请求一个数字。 为什么不简单地为不喜欢遵从指示的人增加一些开销?

确定str和lst范围内最后一个单元格的方法是有缺陷的,但是我相信我已经纠正了它。

 Sub TestRange() Dim val1 As Double, val2 As Double, tmp As Double Dim y As Range, rng As Range, str As Range, lst As Range With Worksheets("Test") Set str = .Range("A2") Set lst = .Range("A" & .Rows.Count).End(xlUp) Set rng = .Range(str, lst) With rng.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorDark1 .TintAndShade = -4.99893185216834E-02 .PatternTintAndShade = 0 End With val1 = Application.InputBox("Please enter the lowest score in your range", "CS2", Type:=1) val2 = Application.InputBox("Please enter the highest score in your range", "CS2", Type:=1) If val2 < val1 Then tmp = val2 val2 = val1 val1 = tmp End If For Each y In rng If y.Value2 >= val1 And y.Value2 <= val2 Then With y.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent2 .TintAndShade = 0.799981688894314 .PatternTintAndShade = 0 End With End If Next y End With End Sub 

tbh,我不知道为什么条件格式与本地工作表公式不是一个更好的解决scheme。 用户input可以调整。