Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have data from A1 to GC15000 on Sheet1; criteria set out on A1:A2 and try copy filtered data to Sheet 3 by pressing the command butotn on Sheet 2,,but I get run time error 1004 (The exact range is a missing or invalid fieldname)


When I try setting the data range and copy to range from A1: Z15000, it works..

Thank you

What I have tried:

Sheets("Sheet1").Range("A1:GC15000").AdvancedFilter Action:=xlFilterCopy, _
 CriteriaRange:=Sheets("Sheet2").Range("A1:A2"), CopyToRange:=Sheets("Sheet3").Range("A1:GC15000"), Unique:=False
Posted
Updated 20-Feb-17 2:58am

1 solution

Please, read this MSDN article[^] to find out what error 1004 means.

Such of addressing: Sheets("Sheet2") may not be enough. Imagine, you have 2 workbooks opened. Each of workbook contains Sheet2. Does the VBA compiler is able to know what sheet you mean? No!

So, replace this:
VB
Sheets("Sheet1").Range("A1:GC15000").AdvancedFilter Action:=xlFilterCopy, _
 CriteriaRange:=Sheets("Sheet2").Range("A1:A2"), CopyToRange:=Sheets("Sheet3").Range("A1:GC15000"), Unique:=False

with:
VB
Sub CopyData()
Dim srcwsh As Worksheet, criwsh As Worksheet, dstwsh As Worksheet

On Error Goto Err_CopyData

Set srcwsh = ThisWorkbook.Worksheets("Sheet1")
Set criwsh = ThisWorkbook.Worksheets("Sheet2")
Set dstwsh = ThisWorkbook.Worksheets("Sheet3")

srcwsh.Range("A1:GC15000").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=criwsh.Range("A1:A2"), CopyToRange:=dstwsh.Range("A1"), Unique:=False

Exit_CopyData:
    Set srcwsh = Nothing
    Set criwsh = Nothing
    Set dstwsh = Nothing
    Exit Sub

Err_CopyData:
    MsgBox Err.Description, vbExclamation, Err.Number
    Resume Exit_CopyData
End Sub


For futher details, please see:
Excel VBA Performance Coding Best Practices - Office Blogs[^]
Handle Run-Time Errors in VBA[^]
Excel 2010 Performance: Tips for Optimizing Performance Obstructions[^]
 
Share this answer
 
v4
Comments
Member 12982752 19-Feb-17 9:43am    
Thank you.. No errors now but it copies only upto Z.. I have column upto GC.. I need this filter to copy upto column GC.. Thanks
Maciej Los 19-Feb-17 9:52am    
Seems, i forgot to add a range to srcwsh. Please check my answer after changes. If it meets your criteria, please accept it (green button).
Member 12982752 19-Feb-17 10:19am    
Thank you.. it perfectly works.. :)
Maciej Los 19-Feb-17 12:31pm    
You're very welcome
Member 12982752 19-Feb-17 10:23am    
I have one more question..I would like to eliminatie the record which has no date ( if there is no date for the record; i.e :- I have date for some records on DF column, just filter only them just eliminate the empty ones)

Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900