Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Excel duplicate Rows

Posted on 2014-03-24
7
149 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 300 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

856 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