if值的VBA代码是<值1个单元格以前在同一列中

时间:2017-12-02 04:26:49

标签: excel vba

我正在寻找VBA代码。

如果列中的任何随机单元格小于同一列中的前一单元格,则放置" A"在E栏中。

即。

Column D    Column E
.01112      A
.01114
.01113
.01112      A
.01114

这是我到目前为止所做的:

Sub ATest()



Dim rngCell As Range, _
        rngDataRange As Range

    Set rngDataRange = Range("D1:D5000")

    rngDataRange.Offset(0, 1).Value = rngDataRange.Value

    For Each rngCell In rngDataRange
        With rngCell

            If .Value > 0.1 And .Value < 0.5 Then
            .Offset(0, 3).Value = .Value 'A[rngCell] to C[rngCell]
            End If
        End With
    Next rngCell
End Sub

1 个答案:

答案 0 :(得分:0)

如果值小于下一个行的值,您的示例意味着您实际上想要在E列中放置“A”,因此添加了额外的If语句你的代码会这样做:

Sub ATest()
    Dim rngCell As Range, _
        rngDataRange As Range

    Set rngDataRange = Range("D1:D5000")

    'This statement doesn't seem to be desired if you want to put the "A"
    'in column E
    'rngDataRange.Offset(0, 1).Value = rngDataRange.Value

    For Each rngCell In rngDataRange
        With rngCell

            If .Value > 0.1 And .Value < 0.5 Then
            .Offset(0, 3).Value = .Value 'A[rngCell] to C[rngCell]
            End If
            If .Value < .Offset(1, 0).Value Then
                .Offset(0, 1).Value = "A"
            End If
        End With
    Next rngCell
End Sub