Solved

Excel duplicate Rows

Posted on 2014-03-24
7
158 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
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.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

622 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