#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Do..Loop


I have found a useful vba (code below) from Ron de Bruin and satisfy my
needs. But the code only solve the one filter criteria. I copied the
following code as many as possible, it is too clumsy. Is there any
method, eg do loop, to solve many filter criteria? In my case, I have
more than 20 criteria.

Sub Copy_With_AutoFilter()
Dim WS As Worksheet
Dim WSNew As Worksheet
Dim Str As String
Sheets("Working").Select
Selection.CurrentRegion.Select
ActiveWorkbook.Names.Add Name:="MyRange",
RefersToR1C1:="=Working! R1C1:R20000C11"

Set WS = Sheets("Working")
Str = "COE"
With WS.Range("MyRange")
..AutoFilter Field:=4, Criteria1:=Str
Set WSNew = Sheets.Add
..Cells.SpecialCells(xlCellTypeVisible).Copy
WSNew.Range("A1")
End With

WS.AutoFilterMode = False
On Error Resume Next
WSNew.Name = Str
If Err.Number 0 Then
MsgBox "Change the name of : " & WSNew.Name & " manually"
Err.Clear
End If
On Error GoTo 0

TIA

norika


--
norika
------------------------------------------------------------------------
norika's Profile: http://www.excelforum.com/member.php...fo&userid=4878
View this thread: http://www.excelforum.com/showthread...hreadid=375507

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 923
Default Do..Loop

Are you asking for multiple filter criteria and/or filter fields?

In any event both can be set by passing values to the autofilter method.
You need to decide where the values are stored. For example you might store
the values in a worksheet range, then you will need to loop through the
range and pass the criteria and field values.

Here is an example of a procedure that is called from the main program to
set a filter up, the criteria is passed in the Value1 string, eg
in main program use....
Call ReadData("36")

' the sub procedure to set filter......
Sub ReadDatal(Value1 As String)
' reset filter if filter on
data.Range("A8:L8").AutoFilter

' turn on filter if not on already on
If Not data.AutoFilterMode Then data.Range("A8:L8").AutoFilter

' set value1 filter if a value1 exists
If Len(Trim(value1)) 0 Then
data.Range("A8:L8").AutoFilter Field:=2, Criteria1:=value1
End If
End Sub

--
Cheers
Nigel



"norika" wrote in
message ...

I have found a useful vba (code below) from Ron de Bruin and satisfy my
needs. But the code only solve the one filter criteria. I copied the
following code as many as possible, it is too clumsy. Is there any
method, eg do loop, to solve many filter criteria? In my case, I have
more than 20 criteria.

Sub Copy_With_AutoFilter()
Dim WS As Worksheet
Dim WSNew As Worksheet
Dim Str As String
Sheets("Working").Select
Selection.CurrentRegion.Select
ActiveWorkbook.Names.Add Name:="MyRange",
RefersToR1C1:="=Working! R1C1:R20000C11"

Set WS = Sheets("Working")
Str = "COE"
With WS.Range("MyRange")
AutoFilter Field:=4, Criteria1:=Str
Set WSNew = Sheets.Add
Cells.SpecialCells(xlCellTypeVisible).Copy
WSNew.Range("A1")
End With

WS.AutoFilterMode = False
On Error Resume Next
WSNew.Name = Str
If Err.Number 0 Then
MsgBox "Change the name of : " & WSNew.Name & " manually"
Err.Clear
End If
On Error GoTo 0

TIA

norika


--
norika
------------------------------------------------------------------------
norika's Profile:

http://www.excelforum.com/member.php...fo&userid=4878
View this thread: http://www.excelforum.com/showthread...hreadid=375507



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
Find loop doesn't loop JSnow Excel Discussion (Misc queries) 2 June 24th 09 08:28 PM
Problem adding charts using Do-Loop Until loop Chris Bromley[_2_] Excel Programming 2 May 23rd 05 01:31 PM
For/Loop skipping one value in loop only Matt Jensen Excel Programming 6 January 8th 05 12:03 PM
Worksheet_Change - loop within a loop bgm Excel Programming 1 January 19th 04 01:27 PM
HELP!!!! Can't stop a loop (NOT an infinite loop) TBA[_2_] Excel Programming 3 December 14th 03 03:33 PM


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