Remove duplicates in all sheets simultaneously through VBA

cgc0512

New Member
Joined
Jul 29, 2022
Messages
1
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I have this code that I used back then for the removal of duplicates in all sheets. When I used it on my current workbook, it suddenly returned with a run-time error '1004' pointing to the range that I defined. What I want to do is to remove all duplicates based off their value in column F. Can somebody please help me revise the code? Also, would it be possible to revise it so that will ignore specific named sheets? Thanks!

VBA Code:
Sub RemoveDuplicates()
 ' RemoveDuplicates Macro
 ' Selects all values then removes duplicates
 '

     ' Declare Current as a worksheet object variable.
         Dim Current As Worksheet
         Dim starting_ws As Worksheet
         Set starting_ws = ActiveSheet 'remember which worksheet is active in the beginning

         ' Loop through all of the worksheets in the active workbook.
         For Each Current In Worksheets
            Current.Activate
            
            ActiveSheet.Range("$A$1:$Q$1000").RemoveDuplicates Columns:=6, Header:=xlYes

         Next
         
    starting_ws.Activate 'activate the worksheet that was originally active
    
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi There

Something like below? Not yet fully tested but this does remove duplicates as specified in Column F and it also excludes Sheet2 and Sheet4...

VBA Code:
Sub RemoveDuplicates()
 ' RemoveDuplicates Macro
 ' Selects all values then removes duplicates
     ' Declare Current as a worksheet object variable.
         Dim Current As Worksheet
         Dim starting_ws As Worksheet
         Set starting_ws = ActiveSheet 'remember which worksheet is active in the beginning
         ' Loop through all of the worksheets in the active workbook.
         For Each Current In Worksheets
             Select Case Current.Name
               Case "Sheet2", "Sheet4"
               'Excluded
               Case Else
            Current.Activate
            ActiveSheet.Range("$A$1:$Q$1000").RemoveDuplicates Columns:=6, Header:=xlYes
    End Select
         Next
    starting_ws.Activate 'activate the worksheet that was originally active
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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