Formula to keep adding a 1 to a cell based on another cell value

Malcolm torishi

Board Regular
Joined
Apr 26, 2013
Messages
219
Hi
can anyone tell me if there is a formula that constantly adds a 1 to a cell value each time a 1 appears in another cell. EG
columnA Row 2:4 has 3 people’s names in each cell , column B row 2:4 as a 1 in each cell, each time the person in column A leaves the building, what I want to do is in column C have a running total of the number of times that person as left the building. So each time a person leaves there will show 1 next to his name in column B and when he returns the 1 gets deleted but if he goes out again a 1 will appear in column B again and I want to constantly keep adding the number of times he goes out to column C
thank you
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
your best bet would be to use the change event on the worksheet.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)


If Not Application.Intersect(Target, Columns(2)) Is Nothing Then
    If Target = 1 Then Target.Offset(0, 1).Value = Target.Offset(0, 1).Value + 1
End If

End Sub
 
Last edited:
Upvote 0
your best bet would be to use the change event on the worksheet.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)


If Not Application.Intersect(Target, Columns(2)) Is Nothing Then
    If Target = 1 Then Target.Offset(0, 1).Value = Target.Offset(0, 1).Value + 1
End If

End Sub


Hi Richard
this Code works perfectly when the value is entered as a digit, but I have a vlookup formula and it just doesn’t want to work. I have even changed the top part of the code to
Private Sub Worksheet _calculate but still does not what to work,
does anyone have any idea what to do please
thank you
 
Upvote 0
Hello can any please help with this been looking all day if an answer , but I just can not sort. I would really appreciate help, all I need is the code to work with a vlookup on my spreadsheet.
 
Upvote 0
Hi
I am still trying to sort this but just can’t. Can anyone help, main issue is getting this to work with a vlookup, can anyone help please . I would so much appreciate it. Thank you in advance.
 
Upvote 0
Here's a possibility that involves using a helper column (which could be hidden).
Assuming column D is the helper column, to set-up enter in C2:C4 the values that apear in B2:B4, then :
Code:
Private Sub Worksheet_Calculate()
Dim rng As Range, cel As Range
Set rng = [C2:C4]
For Each cel In rng
    If cel(1, 0) = 1 And cel(1, 2) = 0 Then
        cel = cel + 1
        cel(1, 2) = 1
    Else: cel(1, 2) = 0
    End If
Next
End Sub
 
Upvote 0
Revised :

Assuming column D is the helper column, to set-up enter in D2:D4 the values that appear in B2:B4, then :
Code:
Private Sub Worksheet_Calculate()
Dim rng As Range, cel As Range
Set rng = [C2:C4]
For Each cel In rng
    If cel(1, 0) = 1 And cel(1, 2) = "" Then
        cel = cel + 1
        cel(1, 2) = 1
    ElseIf cel(1, 0) = "" And cel(1, 2) = 1 Then cel(1, 2) = ""
    End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,620
Messages
6,120,554
Members
448,970
Latest member
kennimack

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top