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

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
560
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
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

Screenshot 2023-01-06 182605.png


VBA Code:
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

 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
VBA Code:
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.
 
Upvote 0
VBA Code:
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.
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.

VBA Code:
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
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
Then it would be something like:
VBA Code:
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
 
Last edited by a moderator:
Upvote 0
Solution

Forum statistics

Threads
1,225,073
Messages
6,182,709
Members
453,132
Latest member
nsnodgrass73

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