支票号码介于范围之间

时间:2016-10-15 06:14:30

标签: excel vba excel-vba macros

This is the format how i want

列A和列C是范围,列B是我必须与列A和列C进行比较的参考值。 例如:(B> A)和(B

基本上我想检查B列是否落在A列和C列之间

这是我准备的代码,但这不起作用,这适用于单个单元格:

Sub a()
Dim x As Integer
Dim y As Integer

    x = Worksheets("Sheet1").Range("A1").Value
    y = Worksheets("Sheet1").Range("B1").Value
    Z = Worksheets("Sheet1").Range("C1").Value

    If Z > x Then
          Worksheets("Sheet1").Range("D1") = "Correct"
    End If
End Sub

2 个答案:

答案 0 :(得分:1)

你可以这样做:

Sub main()
    With Worksheets("Sheet1") 
        With .Range("D1:D" & .Cells(.Rows.Count, 1).End(xlUp).row)
            .FormulaR1C1 = "=IF(AND(RC2>=RC1,RC2<=RC3),""Correct"",""Wrong"")"
            .Value = .Value
        End With
    End With
End Sub

答案 1 :(得分:0)

您可以使用一个简单的公式:

=IF(AND(B1>=A1,B1<=C1),"Correct","Wrong")

enter image description here

如果您仍然需要vba,请使用:

Sub RANGEFALL()
Dim wk As Worksheet, frow As Long, i As Long

Set wk = Sheet1
frow = wk.Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To frow
    If wk.Range("B" & i).Value >= wk.Range("A" & i).Value And wk.Range("B" & i).Value <= wk.Range("C" & i).Value Then
        wk.Range("D" & i).Value = "Correct"
    Else
        wk.Range("D" & i).Value = "Wrong"
    End If
Next i

End Sub