Excel VBA - Autofilter, copy, paste to new sheet

ozalonazol

New Member
Joined
Jun 8, 2017
Messages
2
Hi! My VBA knowledge is very basic so I am really lost about how to solve my problem. I am trying to filter values from a large dataset, copy them and paste them to a new sheet. I found a code in an old threat https://www.mrexcel.com/forum/excel...ications-autofilter-copy-paste-new-sheet.html which i tried to use but it hasn't worked. The code I am using is the following and the error I get is "Run-time error '1004': The extract range has a missing or invalid field name". My data is in the range A1:BH3921 and column A contains the unique values that I want to filter. There are multiple rows containing each value.

Could you please help me to find the error or suggest another code that serves for the same purpose?

Thanks in advance for your help!





Code:
Sub Foo()
    Dim c As Range
    Dim rng As Range
    Dim LR As Long
        
        LR = Cells(Rows.Count, "R").End(xlUp).Row
        Set rng = Range("A1:BH" & LR)
        
        Range("A1:A" & LR).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("AM1"), Unique:=True
        
        For Each c In Range([AM2], Cells(Rows.Count, "AM").End(xlUp))
            With rng
                .AutoFilter
                .AutoFilter Field:=1, Criteria1:=c.Value
                .SpecialCells(xlCellTypeVisible).Copy
                Sheets.Add(After:=Sheets(Sheets.Count)).Name = c.Value
                ActiveSheet.Paste
            End With
        Next c
        
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hello Ozalonazol,

If you look carefully at the code in the old thread, you'll notice that the unique values have been extracted from Column E and copied to Column AM. This is a column outside the data set. In other words, a temporary helper column.

In your code above, you have left the helper column (AM) inside your data set hence creating conflict with the data that should be there and the data which needs to be unique (the criteria that is required to create/name the new sheets).

In this line of code:

Code:
Range("A1:A" & LR).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("AM1"), Unique:=True

change the Column AM reference to a column outside your data set,e.g Column BJ.

In the following line:

Code:
 For Each c In Range([AM2], Cells(Rows.Count, "AM").End(xlUp))

change the column reference from AM to BJ.

See if that does the trick for you.

Cheerio,
vcoolio.
 
Upvote 0
No worries Ozalonazol. Glad that I was able to help.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,215,432
Messages
6,124,858
Members
449,194
Latest member
HellScout

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