# How to insert Columns and Sum when the same Data ? Help VBA



## punnipah (Dec 23, 2022)

Hi,All


I want to insert columns and sum the amounts that have the same data.
-The same data is "Month" in Columns "W"
-Sometimes the data is not sorted.


Ex.my data 








Result


----------



## Peter_SSs (Dec 23, 2022)

punnipah said:


> -Sometimes the data is not sorted.


So, if it isn't sorted, do you want ...
- it sorted (by the Month column) before the totals are inserted?
- it left in the order it is and if, say, month 6512 occurs in 4 separate ranges in column W do you want 4 separate totals for it?

Does the data actually start in column A? If not, where?

BTW, I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with.


----------



## punnipah (Dec 23, 2022)

Peter_SSs said:


> So, if it isn't sorted, do you want ...
> - it sorted (by the Month column) before the totals are inserted?
> - it left in the order it is and if, say, month 6512 occurs in 4 separate ranges in column W do you want 4 separate totals for it?
> 
> ...


- it sorted (by the Month column) before the totals are inserted? = Yes
- it left in the order it is and if, say, month 6512 occurs in 4 separate ranges in column W do you want 4 separate totals for it = Yes
Does the data actually start in column A? If not, where? =Yes Start "A"


----------



## Peter_SSs (Dec 23, 2022)

punnipah said:


> - it sorted (by the Month column) before the totals are inserted? *= Yes*
> - it left in the order it is and if, say, month 6512 occurs in 4 separate ranges in column W do you want 4 separate totals for it *= Yes*


Your first Yes says that if the data is not sorted by the Month column then you want to have it sorted.
Your second Yes says that if the data is not sorted by the Month column then leave it not sorted.
So they cannot both be 'Yes' answers.


----------



## punnipah (Dec 23, 2022)

Peter_SSs said:


> Your first Yes says that if the data is not sorted by the Month column then you want to have it sorted.
> Your second Yes says that if the data is not sorted by the Month column then leave it not sorted.
> So they cannot both be 'Yes' answers.


I so sorry i want to if the data is not sorted by the Month  want to have it sorted


----------



## Peter_SSs (Dec 23, 2022)

punnipah said:


> if the data is not sorted by the Month want to have it sorted


OK, try this with a *copy *of your worksheet.


```
Sub Add_Subtotals()
  With Range("A1").CurrentRegion
    .Sort Key1:=Columns("W"), Order1:=xlAscending, Header:=xlYes
    .Subtotal GroupBy:=23, Function:=xlSum, TotalList:=Array(26), Replace:=True
  End With
End Sub
```


----------



## punnipah (Dec 23, 2022)

Peter_SSs said:


> OK, try this with a *copy *of your worksheet.
> 
> 
> ```
> ...


Thank you very much


----------



## Peter_SSs (Dec 23, 2022)

You're welcome. Thanks for the follow-up.


----------

