Advanced Filter Paste into Excel Table

rilzniak

Active Member
Joined
Jul 20, 2012
Messages
291
Hey everyone,

I've been struggling with a problem and can't seem to come up with a solution so I'm hoping someone here knows a quick fix.

My goal is to use the advanced filter to remove duplicates from column H - MRColLetter - and paste it into the MRTable I've got on another worksheet. The problem is that when I paste the data it not only pastes the header row from the advanced filter in the wrong position - second row of the table - but what I've got also doesn't re-size the table to accommodate the new entries. Below is the code I'm using. Any suggestions?

Code:
Option Explicit

Sub CreateMasterRegionList()

'MR = MasterRegion --> refers to the column on the BranchMaster worksheet
'BM = BranchMaster --> refers to the name of the worksheet

Dim MRColNo As Integer, LastBMRow As Integer
Dim MRColLetter As String, BM As String

BM = "BranchMaster"
' Find what column the MasterRegion head is in
MRColNo = Application.Match("MasterRegion", Worksheets(BM).Rows(1), 0)
'Turn the MasterRegion column number into a letter
MRColLetter = Split(Cells(1, MRColNo).Address, "$")(1)
'Count the number of rows on the BranchMaster worksheet
LastBMRow = Worksheets(BM).Cells(1, MRColNo).End(xlDown).Row

    'Deletes the entries which currently exist in the MRTable and copies the entries in the MR column on the BM worksheet and pastes them in the MRTable without duplicates
    With Range("MRTable").ListObject
        If Not .DataBodyRange Is Nothing Then
            .DataBodyRange.Delete
            .ListRows.Add alwaysinsert:=True
        End If
        Sheets(BM).Columns(MRColLetter).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("MRTable").ListObject, Unique:=True
    End With

End Sub

Thanks in advance,

rilz
 
Last edited:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try with this:


Code:
Sub CreateMasterRegionList()


    'MR = MasterRegion --> refers to the column on the BranchMaster worksheet
    'BM = BranchMaster --> refers to the name of the worksheet
    
    Dim MRColNo As Integer, LastBMRow As Integer
    Dim MRColLetter As String, BM As String
    
    BM = "BranchMaster"
    ' Find what column the MasterRegion head is in
    MRColNo = Application.Match("MasterRegion", Worksheets(BM).Rows(1), 0)
    'Turn the MasterRegion column number into a letter
    MRColLetter = Split(Cells(1, MRColNo).Address, "$")(1)
    'Count the number of rows on the BranchMaster worksheet
    LastBMRow = Worksheets(BM).Cells(1, MRColNo).End(xlDown).Row


    'Deletes the entries which currently exist in the MRTable and copies the entries in the MR column on the BM worksheet and pastes them in the MRTable without duplicates
    With Range("MRTable").ListObject
        
        If Not .DataBodyRange Is Nothing Then
            .DataBodyRange.Delete
            .ListRows.Add alwaysinsert:=True
        End If
        Sheets(BM).Columns(MRColLetter).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("MRTable").ListObject, Unique:=True


[COLOR=#0000ff]        .Resize .Range.CurrentRegion[/COLOR]
    
    End With


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,636
Members
452,662
Latest member
Aman1997

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