Solved

Excel duplicate Rows

Posted on 2014-03-24
7
144 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:Wass_QA
  • 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:Wass_QA
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:Wass_QA
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:Wass_QA
ID: 39950117
Thank you all for the help,
this is exactly what I needed.
thanks
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

757 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

19 Experts available now in Live!

Get 1:1 Help Now