VBA to remove duplicate cell values and format subtotals

bntringa

New Member
Joined
Jun 27, 2023
Messages
1
Office Version
  1. 365
Hi all - first post here.

I have a spreadsheet with subtotals and am looking to remove duplicate values in cells as per the attached. This should run through the entire spreadsheet and remove values where the are the same in columns A:H that are highlighted in red.

Lastly, can I use VBA to bold the "Total" values in column K of each total row?

Thank you!
 

Attachments

  • Excel sample.jpg
    Excel sample.jpg
    174 KB · Views: 8

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hello @bntringa
Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

Try this:
VBA Code:
Sub removeduplicate()
  Dim dic As Object
  Dim rng As Range, f As Range
  Dim lr As Long, i As Long, j As Long
  Dim ky As String, cell As String
  Dim a As Variant
  
  Application.ScreenUpdating = False
  
  lr = Range("A" & Rows.Count).End(3).Row
  Set rng = Range("A" & lr + 1)
  Set dic = CreateObject("Scripting.Dictionary")
  a = Range("A1:H" & lr).Value
  
  For i = 1 To UBound(a, 1)
    ky = ""
    For j = 1 To 8
      ky = ky & Cells(i, j).Value & "|"
    Next
    If Not dic.exists(ky) Then
      dic(ky) = Empty
    Else
      Set rng = Union(rng, Range("A" & i))
    End If
  Next
  rng.EntireRow.Delete
  
  Set rng = Range("K" & lr + 1)
  Set f = Range("A:A").Find("Total", , xlValues, xlPart, , , False)
  If Not f Is Nothing Then
    cell = f.Address
    Do
      Set rng = Union(rng, Range("K" & f.Row))
      Set f = Range("A:A").FindNext(f)
    Loop While f.Address <> cell
  End If
  rng.Font.Bold = True
  
  Application.ScreenUpdating = True
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,976
Members
449,095
Latest member
Mr Hughes

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