Row row values ​​based on start and date of month in another column

Attaching an image for a better understanding of the issue:

enter image description here

Here, column A shows the dates, B has the daily amount, and C has the total amount of each day.

I want to stop the cumulative calculation at the end of each month based on column A and start recalculating the next month. As shown in the image.

I use the code below to find the end of the month and the first day of the month, assigning B = C, but I'm confused how I can start calculating the total amount from the next day for this month.

Appreciate if someone provides me with the logic to achieve this.

Sub MonthInt()

    Dim MaxGain As Workbook
    Dim DailyData As Worksheet
    Dim n As Long, J As Long

    Set MaxGain = Excel.Workbooks("MaxGain.xlsm")
    Set DailyData = MaxGain.Worksheets("DailyData")


   n = DailyData.Cells(Rows.Count, "A").End(xlUp).Row


   DailyData.Range("B2") = DailyData.Range("C2")

   For J = 3 To n

   If DailyData.Range("A" & J) = Application.WorksheetFunction.EoMonth(DailyData.Range("A" & J), 0) Then

   DailyData.Range("C" & J + 1) = DailyData.Range("B" & J)


   End If

    Next
   End Sub
+4
source share
1 answer

Why do you need VBA to get the desired result?

.

...

C2

=B2

C3

=IF(MONTH(A3)<>MONTH(A2),B3,C2+B3)

.

Edit:

VBA, - ...

Sub MonthInt()
    Dim MaxGain As Workbook
    Dim DailyData As Worksheet
    Dim n As Long

    Set MaxGain = Excel.Workbooks("MaxGain.xlsm")
    Set DailyData = MaxGain.Worksheets("DailyData")

    n = DailyData.Cells(Rows.Count, "A").End(xlUp).Row

    With DailyData
        .Range("C2").Value = .Range("B2").Value
        .Range("C3:C" & n).Formula = "=IF(MONTH(A3)<>MONTH(A2),B3,C2+B3)"
        .Range("C3:C" & n).Value = .Range("C3:C" & n).Value
    End With
End Sub

enter image description here

+3

Source: https://habr.com/ru/post/1693893/


All Articles