Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel duplicate Rows

Posted on 2014-03-24
7
Medium Priority
?
160 Views
Last Modified: 2014-03-24
Hello,
Can you please help,
I need to run a macro to remove duplicates and sum values
One line per city per operator.
Please see attached sample.

Your help is appreciated.
thanks
sample.xlsx
0
Comment
Question by:W.E.B
  • 3
  • 2
  • 2
7 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39950011
Have you tried a pivot table?
0
 

Author Comment

by:W.E.B
ID: 39950013
Hello,
no, I didn't try,
I need a macro to add into my already running code.

thanks,
0
 
LVL 19

Expert Comment

by:regmigrant
ID: 39950055
Here's the vba to insert a pivot table which is far the simplest way to achieve what you need:

Sub Macro1()
'
' Macro1 Macro
'

'
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Sheet1!R1C1:R24C3", Version:=xlPivotTableVersion14).CreatePivotTable _
        TableDestination:="Sheet1!R17C7", TableName:="PivotTable1", DefaultVersion _
        :=xlPivotTableVersion14
    Sheets("Sheet1").Select
    Cells(17, 7).Select
    With ActiveWindow
        .Width = 963.75
        .Height = 608.25
    End With
    Range("G17").Select
    With ActiveWindow
        .Width = 1242
        .Height = 608.25
    End With
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Operator")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("City")
        .Orientation = xlRowField
        .Position = 2
    End With
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("Count"), "Sum of Count", xlSum
    With ActiveSheet.PivotTables("PivotTable1")
        .InGridDropZones = True
        .RowAxisLayout xlTabularRow
    End With
    Range("G18").Select
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Operator").Subtotals = _
        Array(False, False, False, False, False, False, False, False, False, False, False, False)
End Sub

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:W.E.B
ID: 39950070
Hello,
thank you for your help,
I get error
run-time error 5
invalid procedure call or argument.

    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Sheet1!R1C1:R24C3", Version:=xlPivotTableVersion14).CreatePivotTable _
        TableDestination:="Sheet1!R17C7", TableName:="PivotTable1", DefaultVersion _
        :=xlPivotTableVersion14

thank you
0
 
LVL 19

Expert Comment

by:regmigrant
ID: 39950080
it works for me! - here's the code in your worksheet
Copy-of-sample.xlsm
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 1200 total points
ID: 39950092
Here is a code without a pivot table.

Sub CombDup()
    Dim cel As Range
    Dim i As Long
    For Each cel In Range(Range("A2"), Range("A2").End(xlDown))
        If IsEmpty(cel) Or IsEmpty(cel.Offset(1)) Then Exit Sub
        For i = cel.End(xlDown).Row To cel.Offset(1).Row Step -1
            If cel.Value = Cells(i, 1) And cel.Offset(, 1).Value = Cells(i, 2) Then
                cel.Offset(, 2).Value = Cells(i, 3) + cel.Offset(, 2).Value
                Range(Cells(i, 1), Cells(i, 3)).Delete shift:=xlUp
            End If
        Next i
    Next cel
End Sub
0
 

Author Closing Comment

by:W.E.B
ID: 39950117
Thank you all for the help,
this is exactly what I needed.
thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

783 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