Macro problem that performs dynamic sums and non-continuous intervals

Asked

Viewed 72 times

1

I have a spreadsheet where column A has following values (1,2,4,6,8,10), and the fields with value 1 should be the sum of all groups 2 below it until the next value with field 1, and group 2 shall be the sum of all values of group 4 below it to the next cell with value 2, and so on to group 8, which shall be the sum of all values 10 below it.

I have already created a structure for all the values of 8 to be filled, but the totalizers of 1 to 6, as they are not continuous, I am not being able to add.

For i = 1 To b
    If balancete.Cells(i + 1, 1) = 10 Then
        cont_10 = cont_10 + 1
    ElseIf balancete.Cells(i + 1, 1) <> 10 Then
        If balancete.Cells(i, 1) = 10 Then
           balancete.Cells(i - cont_10, 4) = "=SOMA(D" & i + 1 - cont_10 & ":D" & i & ")"
           cont_10 = 0
        End If
    Else
        i = i + 1
    End If
Next i

2 answers

1

Try to add up the amounts as you count them.

Dim soma As Double
soma = 0

For i = 1 To b
    If balancete.Cells(i + 1, 1) = 10 Then

        cont_10 = cont_10 + 1
        soma = soma + balancete.Cells(i + 1, 1).Value 'soma os valores a medida com que são localizados

    ElseIf balancete.Cells(i + 1, 1) <> 10 Then
        If balancete.Cells(i, 1) = 10 Then
           balancete.Cells(i - cont_10, 4) = soma 'preenche a celular com o valor total
           cont_10 = 0
        End If
    Else
        i = i + 1
    End If
Next i
  • Thank you very much Felipe, as the database has several columns, I thought to leave the cell filled as formula, so that it was easy to replicate to the other columns of the table, as you mentioned I would have to replicate the fill for many columns, which would take a long time. The above formula fills cells with a sum formula, after which my macro copies the same formula and drags to the other columns.

-1

Thank you very much Felipe, as the database has several columns, I thought to leave the cell filled as formula, so that it was easy to replicate to the other columns of the table, as you mentioned I would have to replicate the fill for many columns, which would take too long.

Dim soma As Double
soma = 0

For i = 1 To b
If balancete.Cells(i + 1, 1) = 10 Then

    cont_10 = cont_10 + 1
    soma = soma + balancete.Cells(i + 1, 1).Value 'soma os valores a medida com que são localizados

ElseIf balancete.Cells(i + 1, 1) <> 10 Then
    If balancete.Cells(i, 1) = 10 Then
       balancete.Cells(i - cont_10, 4) = soma 'preenche a celular com o valor total
       cont_10 = 0
    End If
Else
    i = i + 1
End If
Next i

The formula above fills the cells with a summation formula, after which my macro copies the same formula and drags to the other columns.

  • To thank the colleague, mark his answer as "answer" and if it was very useful click the up arrow!! I suggest deleting this answer, and if so, comment on what it changed in his reply, because it seems that his reply was the same as his.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.