VBA计算基于一列回答添加列

时间:2013-03-27 18:18:46

标签: excel excel-vba vba

到目前为止,我有以下代码从一个列中减去一个列,但是然后需要获取该结果,如果D 否定,那么F = D + F else {{1 }}

E = E + D

我迷路了!在此尝试不同格式的范围并且没有运气 - 帮助!

谢谢!

2 个答案:

答案 0 :(得分:1)

这是你在尝试的吗?

Sub PopCol()
    Dim rng As Range, aCell As Range

    Set rng = Range("D3:D19")

    rng.Formula = "=RC[-1]-RC[-2]"

    For Each aCell In rng
        Select Case aCell.Value

        Case Is < 0 '<~~ If value in D is negative
            '~~>  F = F + D
            aCell.Offset(, 2).Value = aCell.Offset(, 2).Value + aCell.Value
        Case Else
            '~~>  E = E + D
            aCell.Offset(, 1).Value = aCell.Offset(, 1).Value + aCell.Value
        End Select
    Next
End Sub

答案 1 :(得分:0)

尝试以下代码:

  Sub PopCol()
    For i = 3 To 19
        Range("D" & i) = CLng(Range("C" & i) - Range("B" & i))
        If (Range("D" & i) < 0) Then
            Range("F" & i) = Range("D" & i) + Range("F" & i)
        Else
            Range("E" & i) = Range("E" & i) + Range("D" & i)
        End If
    Next
End Sub