Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Error on advanced filter vba excel - 1004

Hi,

I get an error message of:

1004 - No list was found. Select a single cell within your list, and
then click the command again.

When I call the following sub:

Sub ExtractReps()
Dim ws1 As Worksheet
Dim wsNew As Worksheet
Dim rng As Range
Dim r As Integer
Dim c As Range
Set ws1 = Sheets("Sheet1")
Set rng = Range("A:BM")

'extract a list of Sales Reps
ws1.Columns("F:F").Copy _
Destination:=Range("BP1")
ws1.Columns("BP:BP").AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Range("BN1"), Unique:=True
r = Cells(Rows.Count, "BN").End(xlUp).Row

'set up Criteria Area
Range("BP1").Value = Range("F1").Value

For Each c In Range("BN2:BN" & r)
'add the rep name to the criteria area
ws1.Range("BP2").Value = c.Value
'add new sheet and run advanced filter
Set wsNew = Sheets.Add
wsNew.Move After:=Worksheets(Worksheets.Count)
wsNew.Name = c.Value
rng.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Sheets("HRCN_For_Payroll").Range("B P1:BP2"), _
CopyToRange:=wsNew.Range("A1"), _
Unique:=False
Next
ws1.Select
ws1.Columns("BN:BP").Delete
End Sub


When I "debug" it highlights the following snippet of code:
ws1.Columns("BP:BP").AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Range("BN1"), Unique:=True

Can anyone help - if i dont call the sub and just run as macro it all
works fine but i want to include it in vba and when i just try and
call the macro it doesnt work - any ideas would be much appreciated!!

Many thanks

Rachel
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Error on advanced filter vba excel - 1004

It scares me that you have a lot of unqualified range objects in your code.

Range("a:bm")
refers to the activesheet.

I'm not sure that's always what you want.

I qualified the ranges (using with and dots):

Option Explicit
Sub ExtractReps()

Dim ws1 As Worksheet
Dim wsNew As Worksheet
Dim rng As Range
Dim r As Long
Dim c As Range

'extract a list of Sales Reps
Set ws1 = Sheets("Sheet1")
With ws1
Set rng = .Range("A:BM")
.Columns("F:F").Copy _
Destination:=.Range("BP1")

.Columns("BP:BP").AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=.Range("BN1"), Unique:=True

r = .Cells(.Rows.Count, "BN").End(xlUp).Row

'set up Criteria Area
.Range("BP1").Value = .Range("F1").Value

For Each c In .Range("BN2:BN" & r)
'add the rep name to the criteria area
.Range("BP2").Value = c.Value
'add new sheet and run advanced filter
Set wsNew = Sheets.Add
wsNew.Move After:=Worksheets(Worksheets.Count)
wsNew.Name = c.Value
rng.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Sheets("HRCN_For_Payroll").Range("B P1:BP2"), _
CopyToRange:=wsNew.Range("A1"), _
Unique:=False
Next
.Columns("BN:BP").Delete
End With

End Sub

But I'm not sure it refers the worksheets that you wanted.

With ws1
set rng = .range("a:bm")
.....

This dot-range means that it belongs to the last With object--in this case, ws1.


Rachel Curran wrote:

Hi,

I get an error message of:

1004 - No list was found. Select a single cell within your list, and
then click the command again.

When I call the following sub:

Sub ExtractReps()
Dim ws1 As Worksheet
Dim wsNew As Worksheet
Dim rng As Range
Dim r As Integer
Dim c As Range
Set ws1 = Sheets("Sheet1")
Set rng = Range("A:BM")

'extract a list of Sales Reps
ws1.Columns("F:F").Copy _
Destination:=Range("BP1")
ws1.Columns("BP:BP").AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Range("BN1"), Unique:=True
r = Cells(Rows.Count, "BN").End(xlUp).Row

'set up Criteria Area
Range("BP1").Value = Range("F1").Value

For Each c In Range("BN2:BN" & r)
'add the rep name to the criteria area
ws1.Range("BP2").Value = c.Value
'add new sheet and run advanced filter
Set wsNew = Sheets.Add
wsNew.Move After:=Worksheets(Worksheets.Count)
wsNew.Name = c.Value
rng.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Sheets("HRCN_For_Payroll").Range("B P1:BP2"), _
CopyToRange:=wsNew.Range("A1"), _
Unique:=False
Next
ws1.Select
ws1.Columns("BN:BP").Delete
End Sub

When I "debug" it highlights the following snippet of code:
ws1.Columns("BP:BP").AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Range("BN1"), Unique:=True

Can anyone help - if i dont call the sub and just run as macro it all
works fine but i want to include it in vba and when i just try and
call the macro it doesnt work - any ideas would be much appreciated!!

Many thanks

Rachel


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error msg on Advanced Filter Helen Excel Discussion (Misc queries) 0 April 3rd 08 04:52 PM
Advanced Filter - error message Mifty New Users to Excel 2 May 25th 06 07:02 PM
Error using Advanced Filter Unique Records headly Excel Discussion (Misc queries) 5 May 9th 06 01:36 AM
Excel 2003 Macro Error - Runtime error 1004 Cow Excel Discussion (Misc queries) 2 June 7th 05 01:40 PM
Advaced filter error 1004 Tove Excel Worksheet Functions 0 February 28th 05 01:54 PM


All times are GMT +1. The time now is 06:28 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"