删除另一个单元格使用过的单元格的值

时间:2014-04-29 08:45:06

标签: excel excel-vba vba

我想在cell("A1")中输入一个值,然后该单元格将乘以A3的值,新值将替换为A3A1已被清除。

我输入了以下代码,但它只执行整个列,我希望它适用于整个列,行数从20 T2一直到AP2所以42

    Private Sub worksheet_change(ByVal target As Range)
      If target.Column = 20 And target.Value > 0 Then
        Dim val As Double
        val = target.Value
        target.Value = 0
        Cells(target.Row, target.Column - 1).Value = val + Cells(target.Row, target.Column - 1).Value
      End If
    End Sub

3 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

Dim flagProgram As Boolean
Private Sub worksheet_change(ByVal target As Range)
Dim dblResult As Double
If flagProgram = False Then
    flagProgram = True
    dblResult = Cells(1, 1) * Cells(1, 3)
    Cells(1, 1) = ""
    flagProgram = False
End If

End Sub

答案 1 :(得分:0)

你可以使用它。

Private Sub worksheet_change(ByVal target As Range)

  Dim rng As Range
  Dim db As Double
  Set target = Range("a1")
  If target.Value > 0 Then
    db = target.Value * Cells(1, 3)
    MsgBox db
    target.Value = ""
  End If
End Sub

答案 2 :(得分:0)

Private Sub worksheet_change(ByVal target As Range)
  If target.Address = "$A$1" And target.Value > 0 Then
    Dim val As Double
    val = target.Value
    Range("A1").Value = 0
    Cells(3, 1) = val * Cells(3, 1)
  End If
End Sub

添加到Sheet1模块中:

enter image description here

似乎测试好了:

enter image description here