Excel VBA - Macro to delete nearly duplicate rows based on criteria

beese

New Member
Joined
Oct 8, 2014
Messages
1
I have a very large excel table that appears as follows:
BobOH3
BobOH4
BobOH5
MaryOH2
MaryOH3
MaryFLA2
TedAL2
TedAL3
TedNY2
JimNY2

<tbody>
</tbody>
I want to delete rows leaving only the row with the max value in column 3
The final table should appear as follows:
BobOH5Note: Bob max of 3,4,5
MaryOH3Note: Mary OH max of 2, 3
MaryFLA2Note: Mary FLA max
TedAL3Note: Ted AL max of 2,3
TedNY2Note: Ted NY max
JimNY2Note: Jim NY max

<tbody>
</tbody>
I am new to VBA and would appreciate any help you could give me.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
your data is in sheet1 from A1 to c11

Excel Workbook
ABC
1namestatedata
2BobOH3
3BobOH4
4BobOH5
5MaryOH2
6MaryOH3
7MaryFLA2
8TedAL2
9TedAL3
10TedNY2
11JimNY2
Sheet1



I have given three macro. park all the three macros in the standard module
save the file as macro enabled fille. close and open and enable macros

run only the macro "finalmacro" though other macros are parked in the module

Code:
Sub pivottable()
Dim r As Range
Application.DisplayAlerts = False
On Error Resume Next
Worksheets("pivot").Delete


  Sheets.Add
  ActiveSheet.Name = "pivot"
  Worksheets("sheet1").Activate
  Set r = Range("A1").CurrentRegion
  r.Select
       ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        Selection, Version:=xlPivotTableVersion12).CreatePivotTable _
        TableDestination:="pivot!R3C1", TableName:="myPivotTable", DefaultVersion _
        :=xlPivotTableVersion12
    Sheets("pivot").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("myPivotTable").PivotFields("name")
        .Orientation = xlColumnField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("myPivotTable").PivotFields("state")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("myPivotTable").AddDataField ActiveSheet.PivotTables( _
        "myPivotTable").PivotFields("data"), "Sum of data", xlSum
    With ActiveSheet.PivotTables("myPivotTable").PivotFields("Sum of data")
        .Caption = "Max of data"
        .Function = xlMax
    End With
application.displayalerts=true
End Sub

Code:
Sub arranging_data()


Dim rcol As Range, rrow As Range, ccol As Range, crow As Range, ddata As Integer
Dim dest As Range, j As Integer
Worksheets("pivot").Activate
Set rcol = Range(Range("A5"), Cells(Rows.Count, "A").End(xlUp).Offset(-1, 0))
Set rrow = Range(Range("C4"), Cells(4, Columns.Count).End(xlToLeft).Offset(0, -1))
Set dest = Range("a3").End(xlDown).Offset(5, 0)
For Each crow In rrow
For Each ccol In rcol
ddata = Application.Intersect(crow.EntireColumn, ccol.EntireRow)
If ddata = 0 Then GoTo nnext
dest = crow
dest.Offset(0, 1) = ccol
dest.Offset(0, 2) = ddata


Set dest = Cells(Rows.Count, dest.Column).End(xlUp).Offset(1, 0)
Debug.Print dest.Address
nnext:
Next ccol
Next crow




MsgBox "macro done.see sheet pivot and data bottom portion"


Application.DisplayAlerts = True
End Sub



Code:
Sub finalmacro()
pivottable
arranging_data
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top