到目前为止,我有以下代码从另一列中减去一列,但随后需要获取该结果,如果D
为负,则为F = D + F
其他E = E + D
Sub PopCol()
Range("D3:D19").Formula = "=RC[-1]-RC[-2]" 'D=C-B
我迷路了!一直在尝试不同的范围格式,但没有运气-帮助!
谢谢你!
这是你正在尝试的吗?
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
试试下面的代码:
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