2

到目前为止,我有以下代码从另一列中减去一列,但随后需要获取该结果,如果D,则为F = D + F其他E = E + D

  Sub PopCol()
      Range("D3:D19").Formula = "=RC[-1]-RC[-2]"  'D=C-B 

我迷路了!一直在尝试不同的范围格式,但没有运气-帮助!

谢谢你!

4

2 回答 2

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
于 2013-03-27T18:42:25.473 回答
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
于 2013-03-27T18:55:31.960 回答