VBA,如果值在范围1中,那么值在范围2中乘以-1

时间:2017-06-07 19:20:36

标签: excel vba excel-vba

这是excel中的C列和G列

($12,843.63)    40100
($9,584.60)     53450
($67,933.38)    53500
($2,615.56)     54900
$43,378.11      72100
$607.91         72100
$14,843.94      72400
$286.62         72440
$3,283.73       72500
$46.50          72500
$9,392.28       75400
$3,754.16       81100
$540.00         81300
$6,186.20       82110
$4,658.21       82140
$13,576.61      82160
$35,467.33      82200
$196.18         82600
$10.83          83100
$34.84          83140
$22,535.60      85240
$691.47         85260
$145.00         85300
($45.00)        85980
$1,010.00       86500
$32,249.97      99150
($8,992.44)     99200
$0.00           72100

'如果第二个范围中的值介于40000和60000之间,则第一个范围中的值乘以-1

Dim rng As Range
Dim rngsear As Range
Set rng = Range("D15:D" & lastrow)
Set rngsear = Range("C15:C" & lastrow)
If rng.Value >= (40000 < 60000) Then
rngsear.Value = -rngsear.Value
End If

它说我有一个运行时错误&#39; 91&#39; 对象变量或未设置块变量 我不确定如何让它正常循环。

1 个答案:

答案 0 :(得分:0)

无循环地做到这一点:

Sub change()
Dim rng As Range
Dim rngsear As Range
Dim lastrow As Long
With ActiveSheet
    lastrow = .Range("D" & .Rows.Count).End(xlUp).Row
    Set rng = .Range("D15:D" & lastrow)
    Set rngsear = .Range("C15:C" & lastrow)
    rngsear.Value = .Evaluate("IF((" & rng.Address & " >= 40000)*(" & rng.Address & " < 60000)," & rngsear.Address & " * -1," & rngsear.Address & ")")
End With

End Sub