VBA: Remove Range for Dynamic Named Range

Seba Robles

Board Regular
Joined
May 16, 2018
Messages
73
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello,

I am trying to use VBA to remove duplicates in a named range but I can't get it to work.

My dataset has 7 columns, A through G. Column A is the one with the unique key.

The line I'm trying to use to remove duplicates is
VBA Code:
SourceRng.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6), Header:=xlYes
I think it's not really finding duplicates because it's considering all other columns (A through G)? However, I don't know how to get that part down.

Here is my full code --- line 73 has where I'm trying to remove duplicates:

VBA Code:
Sub UpdateData()

Dim Confirm As Integer
Confirm = MsgBox("Do you wish to update the file?", vbYesNo)

If Confirm = vbYes Then
     
    'Unhide Raw Data worksheet
    Sheets("Raw Data").Visible = True
 
    'Declare and set variables
    Dim Summary_ws As Worksheet: Set Summary_ws = ThisWorkbook.Worksheets("Summary")
    Dim Source_ws As Worksheet: Set Source_ws = ThisWorkbook.Worksheets("Source")
    Dim RawData_ws As Worksheet: Set RawData_ws = ThisWorkbook.Worksheets("Raw Data")
    Dim ExportRng As Range: Set ExportRng = Summary_ws.Range("ExportData")
    Dim SourceRng As Range: Set SourceRng = Source_ws.Range("SourceData")
 
    Application.StatusBar = "Updating data, please wait..."
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
 
    RawData_ws.Activate
 
    'Remove all active filters
    On Error Resume Next
    RawData_ws.ShowAllData
 
        'Delete previous raw data
        Range("A3").EntireRow.Select
        Range(Selection, Selection.End(xlDown)).Delete Shift:=xlUp
        Range("A2:D2").ClearContents
 
    'Search and select CO data Excel file
    Application.DefaultFilePath = "C:"
    f = Application.GetOpenFilename("ExcelWorkbook(*.*),*.*,", , "Select CO file to import")
    If f = "False" Then Exit Sub
 
    'Load Data
    Set W = Workbooks.Open(f)
    Set W1 = W.ActiveSheet
 
        'Copy CO list
        W1.Activate
        Range("A5").End(xlDown).Offset(-1).Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlUp)).Offset(1).Copy
 
        'Paste CO list into Master Roster file
        RawData_ws.Activate
        Range("A2").PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
     
        W.Close False

        'Extend formulas
        Range("A1").End(xlDown).Offset(0, 9).Select
        Range(Selection, Selection.End(xlToLeft)).Offset(0, 1).Select
        Range(Selection, Selection.End(xlUp)).FillDown
     
    'Crosscheck imported CO list against Source worksheet
    Range("E2:H2").Select
    Range(Selection, Selection.End(xlDown)).Copy
    Source_ws.Activate
    Range("B1").End(xlDown).Offset(1).PasteSpecial Paste:=xlPasteValues
 
        'Refresh CONCATENATE column
        Range("A3").Select
        Range(Selection, Selection.End(xlDown)).ClearContents
        Range("B1").End(xlDown).Offset(0, -1).Select
        Range(Selection, Selection.End(xlUp)).FillDown
 
    'Remove duplicates
    SourceRng.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6), Header:=xlYes
 
        'Sort table A to Z
        With Source_ws
            SourceRng.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
        End With
 
    'Refresh all pivot tables
    ThisWorkbook.RefreshAll
    ThisWorkbook.RefreshAll
     
    'Hide Raw Data worksheet
    RawData_ws.Visible = False
 
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    Application.StatusBar = "Data succesfully updated."
 
    Summary_ws.Activate
    Range("A1").Select
 
    MsgBox ("File has been succesfully updated.")
 
Else 'Do nothing

End If

End Sub

Let me know if you need additional information.

Thanks in advance!
 
Last edited by a moderator:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try it like
VBA Code:
SourceRng.RemoveDuplicates Columns:=1, Header:=xlYes
 
Upvote 0
Solution
Thank you! I did that without using the named range and it worked. For some reason it wasn't working when I used the named range.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,123
Members
452,381
Latest member
Nova88

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