Solved

vba remove duplicates and sum the value

Posted on 2010-09-13
7
838 Views
Last Modified: 2012-05-10
I have table as below

date     -         value      -       units
-------------   ------------      ------------
02/02/2000     32.30            23
03/02/2000     22.30            20
03/02/2000     62.30            21

if there more than one occurance of same date, i need to retain just one occurance and sum the values on same date

hence above table will look as below

date     -         value      -       units
-------------   ------------      ------------
02/02/2000     32.30            23
03/02/2000     84.60            41
0
Comment
Question by:cynx
  • 3
  • 2
  • 2
7 Comments
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 33661723
Hello cynx,

this looks like the perfect scenario for a pivot table.

Get started here: http://peltiertech.com/Excel/Pivots/pivotstart.htm


or post a workbook, so we can help you out in your own file.

Post only dummy data, no confidential data, please.

cheers, teylyn
0
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 33661762
see attached for a pivot table example based on your data

cheers, teylyn
pivot.xls
0
 
LVL 59

Accepted Solution

by:
Saurabh Singh Teotia earned 500 total points
ID: 33661814
Assuming your dates are in A Column starting from row-1 and you want to sum column b and column c values then you can use the following code..
Saurabh...

Sub moddata()

    Dim rng As Range, cell As Range, r As Range

    Dim lrow As Long, i As Long

    

    Dim rng1 As Range, rng2 As Range

    

    lrow = Cells(Cells.Rows.Count, "a").End(xlUp).Row



    Set rng = Range("A2:A" & lrow)

    Set rng1 = Range("b2:b" & lrow)

    Set rng2 = Range("c2:c" & lrow)

i = 2



Do Until i > Cells(Cells.Rows.Count, "a").End(xlUp).Row







        Set r = Range("A2:A" & i)



        If Application.WorksheetFunction.CountIf(r, Cells(i, "a")) > 1 Then

            Rows(i).Delete

            

            

        Else

            Cells(i, "b").Value = Application.WorksheetFunction.SumIf(rng, DateValue(Cells(i, "A").Value), rng1)

            Cells(i, "c").Value = Application.WorksheetFunction.SumIf(rng, DateValue(Cells(i, "A").Value), rng2)



i = i + 1



        End If

    Loop







End Sub

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:cynx
ID: 33661864
thanks guys, yes i am familiar with pivot tables, but i require this to be done thru vba since i am working on a macro and i am required to convert the files first in above format.

I will try saurabh's code and get back !
0
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 33661887
@Saurabh...

Why use VBA when native Excel can provide the same functionality much more efficiently?

Isn't that counter-productive and against good practice and spreadsheet design?

Many askers want a VBA solution (apparently), but it seems they don't know about the functionality Excel offers without macros. Just because it can be done with VBA does not mean it's the best way to do it.

I'd go for the pivot table over a VBA solution any time. It's definitely faster and more flexible.

cheers, teylyn
0
 
LVL 59

Expert Comment

by:Saurabh Singh Teotia
ID: 33661913
teylyn,
I thought about pivot table as well but when i re-read and tag zones he head visual basic programming and which gave me a feeler that he is looking for code solution which is just coming by my experience and that;s the reason a code..
Saurabh...
0
 
LVL 1

Author Comment

by:cynx
ID: 33665143
Thanks !

@teylyn: i preferred VBA solution, since i need to use these sheets as input to my macro. if there are 100s of such sheets, a click of button and let the code behind do the job is more productive rather user creating pivot for each !

@saurabh: the code works perfect as i require !

cheers,
mehul
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Find word and 6 digit number 22 97
OCT or Config.xml 2 34
Create Form using Wizard 14 32
In this macro, I would like to be prompted for a folder. 7 20
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
My experience with Windows 10 over a one year period and suggestions for smooth operation
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now