VBA sum data in certain columns based on common data

Forrtis

New Member
Joined
Apr 4, 2018
Messages
18
I have a very similar problem to the one in the link below:

https://www.mrexcel.com/forum/excel...plicate-rows-sum-values-certain-column-4.html

The only difference is the formatting of the data. The unsorted data comes in the following format:

GHIJKLMN
1
IDNameBlank columnBlank ColumnBlank ColumnQuantityUnit priceTotal
223X1Y15L*M
341X2Y215L*M
441X2Y315L*M
523X1Y45L*M
641X2Y515L*M

<tbody>
</tbody>


The expected results is similar to the quoted thread, although there are slight differences:
  • Columns L and N need to be summed
  • Columns I, J and K need to stay blank
  • Any number of rows may contain common data in cells belonging to columns G, H and M
    • Values in Column M may be common for two different ID's therefore this column cannot be used as a guide to consolidate the data
    • Either Column G or H can be used to look for common data and then the common rows need to be summed - only summing the columns indicated above
  • There are ~100 different ID's to work with so the code needs to be able to handle this



GHIJKLMN
1
IDNameBlank columnBlank ColumnBlank ColumnQuantityUnit priceTotal
223X1Y1+Y45L*M
341X2Y2+Y3+Y515L*M

<tbody>
</tbody>

Any help would be greatly appreciated!
****** id="cke_pastebin" style="position: absolute; top: 573px; width: 1px; height: 1px; overflow: hidden; left: -1000px;">
L*M

<tbody>
</tbody>
aNYa</body>
 
That works exactly how I planned for the tool to work! Thank you very much, I am new to this forum is there anything I can do to show my appreciation?
 
Last edited:
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You're very welcome
If you feel so inclined you could a donation to a charity of your choice.
Regrds Mick
 
Upvote 0
Hi MickG, I have tried adding a
Code:
With Worksheets("worksheet name")
.......statement into this code so that it carries out this code within a specific worksheet, however this brings up errors that I can't seem to get past? Is there a simple line of code that I can add so that this code works on the data within a certain worksheet?
 
Upvote 0
Try changing this :-
Code:
Set Rng = Range("G2", Range("G" & Rows.Count).End(xlUp))

To this:-
NB:- Notice the Dots in red ???
Code:
With sheets("Sheetname")
     Set Rng [SIZE=3][COLOR="#FF0000"][B].[/B][/COLOR][/SIZE]Range("G2", [COLOR="#FF0000"][SIZE=3][B].[/B][/SIZE][/COLOR]Range("G" & Rows.Count).End(xlUp))
end with
 
Last edited:
Upvote 0
That nearly works, the code looks at data in the specified worksheet but the result is still printed onto the first worksheet within the workbook; not the specified worksheet.
 
Upvote 0
Alter last bit of code as below:-
Change Sheet Name and range "A1" to suit.
Code:
With sheets("SheetName").Range("A1").Resize(c, 8)
    .Value = Ray
    .Borders.Weight = 2
End With
 
Upvote 0
Thank you very much, it worked!

One more thing, when there is a gap in the data i.e an empty row (this happens quite often) the code counts the the empty rows as an item and tries to sum up the data. Which means that the result includes an empty row at the top of the list with "0" in QTY and Amount columns. Is there a way to modify this code so it ignores blank rows that exist in-between the actual data that I am trying to sum?
 
Upvote 0
Try adding lines in red
Code:
For Each Dn In Rng
 [COLOR="#FF0000"][B]If not IsEmpty(dn.value) then 
[/B][/COLOR] If Not .Exists(Dn.Value) Then
        c = c + 1
        For Ac = 1 To 7
            Ray(c, Ac) = Dn.Offset(, Ac - 1)
        Next Ac
        Ray(c, 8) = Dn.Offset(, 5) * Dn.Offset(, 6)
        .Add Dn.Value, c
    Else
        Ray(.Item(Dn.Value), 6) = Ray(.Item(Dn.Value), 6) + Dn.Offset(, 5)
        Ray(.Item(Dn.Value), 8) = Ray(.Item(Dn.Value), 8) + Dn.Offset(, 5) * Dn.Offset(, 6)
    End If
[COLOR="#FF0000"][B]End If [/B][/COLOR]
Next
 
Upvote 0
The code has thrown up an error today "Subscript out of range".
I have recently added some delete buttons to my workbook which delete a range of data from the worksheet that the code looks at to sum the common rows.
Once deleted the code also re-runs the code in this thread. So everything works fine up until there are no more duplicate rows for the code to sum, at this point the "Subscript out of range" error comes up highlighting the following line of code
Code:
Ray(C, Ac) = Dn.Offset(, Ac - 1)
.

Please note: The code also works fine if the first row of the range that the code is looking to sum is blank. Please see attached images for more clarification!

https://imgur.com/a/hjxieYF
 
Upvote 0
I note that the code looks for data starting column "G" and your Data start column "I" ???
Can you send a copy of data/results file (fails not fails) Using "Box.com" or "Dropbox.com" as a picture is not very helpful !!
Regrds Mick
 
Upvote 0

Forum statistics

Threads
1,215,294
Messages
6,124,100
Members
449,142
Latest member
championbowler

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