如果那么在范围vba

时间:2016-07-03 19:20:38

标签: excel vba excel-vba

我正在尝试使用此vba代码来识别范围之间的值,然后在满足条件时为单元格着色,但是我无法使if then语句正确。

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

2 个答案:

答案 0 :(得分:2)

InputBox返回String,您永远不会将返回值强制转换为数字类型。这意味着您正在执行字符串比较,而非数字比较。如果其中一个字符串比另一个字符串长,它只会根据字符代码比较较短字符串中的字符数:

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

首先需要验证用户在InputBox中键入的内容实际上是一个数字,然后将其转换为数字类型, 然后 执行比较。我也摆脱了Goto并以一种用户不必重新输入有效值的方式构造输入序列:

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

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

答案 1 :(得分:1)

有一个Range.Value2 property。尽量不要重新使用保留字,特别是当存在模糊方法时。

Excel应用程序InputBox method允许您专门请求一个号码。为什么不简单地为那些不喜欢遵循指示的人增加一些开销呢?

确定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,我不知道为什么使用原生工作表公式的Conditional Formatting不是更好的解决方案。用户输入可以调整为。