excelvbarangecell

Adding the value 3 in a cell when another cell changes to 1


I shall monitor the value of a robot in excel. Everytime the robot sends the value "1" to my computer (Switches between 1 and 0, because digital) which shows up in the cell A1 in excel, the value "3" shall be added in cell B1. Second time A1 changes to 1 the value shall be 6 in B1 etc.

The code below does just that, except that it adds the value 3 even when it switches FROM 1 TO 0. I only want it to switch when it changes FROM 0 TO 1.

Using Excel 2007

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("A5")) Is Nothing Then Me.Range("B5").Value = Me.Range("B5").Value       + 3

Solution

  • If target.Column = 1 and target.Row = 1 then
        If target.Value = 1 Then
            Range("B1").Value = Range("B1").Value + 3
        End If
    End if
    

    This should do it.