Deleting rows based on values in next row

snowyday

New Member
Joined
Feb 8, 2018
Messages
3
Hi All,
I have a file from our SAP system that exports our company's organizational structure. It contains rows for each individual org relationship. I need to send this data to an external vendor that only wants one row per relationship, so they don't have to process extra rows.
The example below shows how the data comes out of SAP. You can see that Executive Leadership is repeated on lines 1, 2 and 3, and that Chief Financial Officer is repeated on lines 2 and 3. What I need to provide to the external vendor is line 3, which contains the entire reporting relationship.

I'm looking for some code that will compare line 1 with line 2 to see if all of the values exist, if they do, then delete the row or mark it for delete somehow. Then it take the next row and compare it to the one after. I hope I'm making sense....

The real file has eight levels of organization units and over 1000 rows.

Thanks



Lvl 1 IdLvl 1 NameLvl 2 IdLvl 2 NameLvl 3 IdLvl 3 Name
55623544Executive Leadership
55623544Executive Leadership98564755Chief Financial Officer
55623544Executive Leadership98564755Chief Financial Officer3255445732554457

<tbody>
</tbody>
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
How about
Code:
Sub DeleteLevels()

   Dim Cl As Range
   Dim Rng As Range
   
   Set Rng = Range("A" & Rows.Count).End(xlUp).Offset(1)
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then
            .Add Cl.Value, Cl
         ElseIf WorksheetFunction.CountA(Rows(Cl.Row)) > WorksheetFunction.CountA(Rows(.Item(Cl.Value).Row)) Then
            Set Rng = Union(Rng, .Item(Cl.Value))
            Set .Item(Cl.Value) = Cl
         Else
            Set Rng = Union(Rng, Cl)
         End If
      Next Cl
   End With
   If Not Rng Is Nothing Then Rng.EntireRow.Interior.Color = vbRed
End Sub
This will currently just highlight the rows to be deleted, if it's working ok then change the last line to this
Code:
   If Not Rng Is Nothing Then Rng.EntireRow.Delete
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,690
Members
449,117
Latest member
Aaagu

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