LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Advanced Filter Range Selection in VB


Good evening,

I have been racking my brain for the past few weeks off and on trying
to fix some VB code to do what I want it to do. Below, you will see
the VB code I am using. This code was taken from a sample file at an
excel tips website. I have no VB background but I have tweaked this
code to do 90% of what I need it to do. The only thing I can not
figure out is how to get it to select a larger number of rows to bring
into the Advance Filter. The macro will take in only the first 43 rows
of the spreadsheet. If I add more rows to the spreadsheet, it will not
take them into account when the macro runs the Advance Filter.

I have discovered that when I open the file before even running the
macro, the range shows up as A1 through G43 in the Advanced Filter
dialog box via the Data option on the menu bar. Therefore, it may not
be the VB code that is the problem.

Is there some reason why the program is only picking up 43 rows and not
the rest when I add more rows?

The following code is from the sample file located at
http://www.contextures.com/AdvFilterRepFiltered.xls :

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("Database")

'extract a list of Sales Reps
ws1.Columns("C:C").Copy _
Destination:=Range("L1")
ws1.Columns("L:L").AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Range("J1"), Unique:=True
r = Cells(Rows.Count, "J").End(xlUp).Row

'set up Criteria Area
Range("L1").Value = Range("C1").Value

For Each c In Range("J2:J" & r)
'add the rep name to the criteria area
ws1.Range("L2").Value = c.Value
'add new sheet (if required)
'and run advanced filter
If WksExists(c.Value) Then
Sheets(c.Value).Cells.Clear
rng.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Sheets("Sheet1").Range("L1:L2"), _
CopyToRange:=Sheets(c.Value).Range("A1"), _
Unique:=False
Else
Set wsNew = Sheets.Add
wsNew.Move After:=Worksheets(Worksheets.Count)
wsNew.Name = c.Value
rng.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Sheets("Sheet1").Range("L1:L2"), _
CopyToRange:=wsNew.Range("A1"), _
Unique:=False
End If
Next
ws1.Select
ws1.Columns("J:L").Delete
End Sub
Function WksExists(wksName As String) As Boolean
On Error Resume Next
WksExists = CBool(Len(Worksheets(wksName).Name) 0)
End Function




Thank you for any help you can give!!!

Hulk


--
Hulk
------------------------------------------------------------------------
Hulk's Profile: http://www.excelforum.com/member.php...o&userid=14947
View this thread: http://www.excelforum.com/showthread...hreadid=265799

 
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
Advanced Filter using a range name as the criteria Joe Excel Worksheet Functions 1 December 27th 08 06:23 PM
advanced filter a range Il Principe Excel Worksheet Functions 2 August 1st 05 03:27 PM
Advanced filter and Criteria Range gearoid Excel Discussion (Misc queries) 2 July 20th 05 02:33 PM
"Criteria Range" in the "Data/Filter/Advanced Filter" to select Du TC Excel Worksheet Functions 1 May 12th 05 02:06 AM
Advanced Filter & Named Range LS[_2_] Excel Programming 3 April 7th 04 02:28 AM


All times are GMT +1. The time now is 11:13 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"