# Sum cell values with the month condition and a value in a column



## sofas (Jan 6, 2023)

Hello, I want to put a condition when moving to a new month or when checking for the presence of the word ok in column A
The counting process is stopped and restarted
This is an example of the expected resultThis is an example 







```
Sub Progressive_Product_v2()
  Dim a As Variant
  Dim i As Long
 
  With Range("q7", Range("q" & Rows.Count).End(xlUp))
    a = .Value
    For i = 2 To UBound(a)

      a(i, 1) = a(i, 1) + a(i - 1, 1)
    Next i
    .Offset(, 1).Value = a

  End With
  With Range("R7", Range("R" & Rows.Count).End(xlUp))
    a = .Value
    For i = 2 To UBound(a)

      a(i, 1) = a(i, 1) + a(i - 1, 1)
    Next i
    .Offset(, 1).Value = a

  End With
 
End Sub
```









						TEST1(2)
					

MediaFire is a simple to use free service that lets you put all your photos, documents, music, and video in a single place so you can access them anywhere and share them everywhere.



					www.mediafire.com


----------



## Flashbond (Saturday at 3:40 PM)

```
Sub myFunction()
  Dim lRow As Integer
  lRow = Cells(Rows.Count, 4).End(xlUp).Row
 
  For i = 7 To lRow
    If Month(Cells(i, 4).Value) <> Month(Cells( i- 1, 4).Value) And Cells(i, 2).Value = "ok" Then
      Cells(i, 18).Value = Cells(i, 20).Value
    Else
      Cells(i, 18).Value = Cells(i - 1, 18).Value + Cells(i, 20).Value
    End If
  Next
End Sub
```
I didn't understand the calculation with column Q.


----------



## sofas (Saturday at 4:58 PM)

Flashbond said:


> ```
> Sub myFunction()
> Dim lRow As Integer
> lRow = Cells(Rows.Count, 4).End(xlUp).Row
> ...


Thank you for your interest. This solution has been reached and it works well. Now I lack how to implement the same command on the adjacent column (S) to calculate the values of the (R) column.


```
Sub compte()
dblg = 7
n = 0
compteur = 0
mois = 0
While Cells(dblg + n, 17) <> ""

If Month(Cells(dblg + n, 4)) <> mois Then
compteur = 0
mois = Month(Cells(dblg + n, 4))

End If
If Cells(dblg + n, 4) = "ok" Then compteur = 0
compteur = compteur + Cells(dblg + n, 17)
Cells(dblg + n, 18) = compteur
n = n + 1
Wend

End Sub
```


----------



## Flashbond (Saturday at 5:06 PM)

What does column S calculate? I don't get it.
Also I don't understand your VBA method. Again I will offer in my own notation. You can modify it. But I have to know what column S calculates.


----------



## sofas (Saturday at 5:12 PM)

Flashbond said:


> What does column S calculate? I don't get it.
> Also I don't understand your VBA method. Again I will offer in my own notation. You can modify it. But I have to know what column S calculates.


Calculates the values of the R column Cell by cell, provided the date and the word OK If the month is changed, it starts from the cell containing the date of the new month


----------



## Flashbond (Saturday at 5:26 PM)

No, I mean let say is S1 = R1 + R2?


----------



## Flashbond (Saturday at 5:51 PM)

Then it would be something like:

```
Sub myFunction()
  Dim lRow As Long, sPoint As Long
  lRow = Cells(Rows.Count, 17).End(xlUp).Row
 
  For i = 7 To lRow
    If Month(Cells(i, 4).Value) <> Month(Cells( i- 1, 4).Value) And Cells(i, 2).Value = "ok" Then
      Cells(i, 18).Value = Cells(i, 20).Value
      Cells(i, 19).Value = Cells(i, 18).Value
      sPoint = i
    Else
      Cells(i, 18).Value = Cells(i - 1, 18).Value + Cells(i, 20).Value
      Cells(i, 19).Value = WorksheetFunction.Sum(Cells(sPoint, 18), Cells(i, 18))
    End If
  Next
End Sub
```


----------

