dimanche 8 août 2021

Sum column values based on condition

I'm an intern and I've been struggling to find a solution since this monday... I'm new to VBA and I don't clearly see how I can sum cells from a column based on some conditions..I tried multiple code but as soon as my codes didnt work I deleted them.

So what I'm trying to do is the following;

I've got a worksheet called worksheets("Amounts") in which I've got a data base.

What I've been struggling to do since this Monday : Sum the amounts value in column Q ( "AMOUNT") Only if rows of COL A, col B, col C, col , col E, col F have equivalent cells value.

Then, I'd like to sum in col Q the amounts based on the previous condition and put the total in one single row in the place of the rows that contain common values. Right after I'd like to delete each rows that were matching to one another to display the agregated amount with the common values. Like the following example;

My data base;

COL A COL B COL C COL E COL F COL Q
CODE STATUE ATTRIBUTE Country Capital AMOUNT
A1 OK Z1 ENGLAND LONDON 400
C1 NOK R2 SPAIN MADRID 50
A1 OK Z1 ENGLAND LONDON 300
D1 PENDING X CANADA OTTAWA 10

the Output expected;

COL A COL B COL C COL E COL F COL Q
CODE STATUE ATTRIBUTE Country Capital AMOUNT
A1 OK Z1 ENGLAND LONDON 700
C1 NOK R2 SPAIN MADRID 50
D1 PENDING X CANADA OTTAWA 10

==> So here we have only 2 rows with common value on col A, B, C, E and F. I'd like to sum the amounts of these two rows and delete these two rows to make a single one with these common values like the up-above example.

Obviously for the other rows that dont match with other rows I'd like to let them as they were.

the database in worksheets("Amount") can vary and can get more or less rows, so I will need to automatize this process.

Here is my last saved code:

Option Explicit

Sub agreg()
Dim i As Long
Dim ran1 As Range

ran1 = ThisWorkbook.Worksheets("Values").Range("A" & Worksheets("Values").Rows.Count).End(xlUp).row + 1
For Each i In ran1
   If Cells(i, 1) = Range("A1", Range("A1").End(xlDown)).Value Then
       cells(i,4) + range("D1",range("D1").End(xlDown)).Value
       
    End If
Next i

   
End Sub  ```

Aucun commentaire:

Enregistrer un commentaire