Advanced Filter Paste into Excel Table

rilzniak

Active Member
Joined
Jul 20, 2012
Messages
288
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

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
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,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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