Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default vba filter code

Hi,

I'm thinking to write a vba code in excel that filters a table based
on a precise criteria. For instance, I have a Table on Sheet1 that
looks like this:

a1 b1 c1
a2 b2 c2
a3 b3 c3

The macro should delete those rows that do not contain either a2 (on
column1) or b3 (on column2). In this example, the first row will be
the only one left.
Can anyone help ?

Thank you
Gabriel
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default vba filter code

The easiest would be to put in a helper column

Sub AAABB()
Dim rng As Range
Set rng = Range(Cells(1, 1), Cells(1, 1).End(xlDown))
rng.Offset(0, 3).Formula = "=if(Or(A1=""a2"",B1=""b3"")," & _
"""Keep"",""Delete"")"
Rows(1).Insert
Range("A1").Resize(1, 4).Value = Array("AA", "BB", "CC", "DD")
Range("A1").AutoFilter Field:=4, Criteria1:="Delete"
With ActiveSheet.AutoFilter.Range
.Columns(1).SpecialCells(xlVisible).EntireRow.Dele te
End With
ActiveSheet.AutoFilterMode = False
Columns(4).Delete

End Sub

--
Regards,
Tom Ogilvy





"Gabriel" wrote in message
om...
Hi,

I'm thinking to write a vba code in excel that filters a table based
on a precise criteria. For instance, I have a Table on Sheet1 that
looks like this:

a1 b1 c1
a2 b2 c2
a3 b3 c3

The macro should delete those rows that do not contain either a2 (on
column1) or b3 (on column2). In this example, the first row will be
the only one left.
Can anyone help ?

Thank you
Gabriel



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 171
Default vba filter code

A different approach is available if the functions in the freely
downloadable file at http://home.pacbell.net/beban are available to your
workbook:

=ArrayRowFilter2(ArrayRowFilter1(Table_Range,1,"a2 ","<"),2,"b3","<")

array entered into a range of at least sufficient size to accommodate
the output.

Alan Beban

Tom Ogilvy wrote:
The easiest would be to put in a helper column

Sub AAABB()
Dim rng As Range
Set rng = Range(Cells(1, 1), Cells(1, 1).End(xlDown))
rng.Offset(0, 3).Formula = "=if(Or(A1=""a2"",B1=""b3"")," & _
"""Keep"",""Delete"")"
Rows(1).Insert
Range("A1").Resize(1, 4).Value = Array("AA", "BB", "CC", "DD")
Range("A1").AutoFilter Field:=4, Criteria1:="Delete"
With ActiveSheet.AutoFilter.Range
.Columns(1).SpecialCells(xlVisible).EntireRow.Dele te
End With
ActiveSheet.AutoFilterMode = False
Columns(4).Delete

End Sub


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default vba filter code

If you only want row 1 remaining, you would alter the helper column formula
to be


rng.Offset(0, 3).Formula = "=if(NOT(Or(A1=""a2"",B1=""b3""))," & _
"""Keep"",""Delete"")"

Your stated expectation, that only row 1 would remain, contradicts your
description of what you want deleted.

to match your expected results, I believe the condition would be to delete
any row that contains A2 in column 1 or B3 in column 2

to me, what you stated doesn't say that:
The macro should delete those rows that do not contain either a2 (on

column1) or b3 (on column2).

difference between "do not" and "do"

Anyway, the code can be altered to do either as I showed.

--
Regards,
Tom Ogilvy


"Tom Ogilvy" wrote in message
...
The easiest would be to put in a helper column

Sub AAABB()
Dim rng As Range
Set rng = Range(Cells(1, 1), Cells(1, 1).End(xlDown))
rng.Offset(0, 3).Formula = "=if(Or(A1=""a2"",B1=""b3"")," & _
"""Keep"",""Delete"")"
Rows(1).Insert
Range("A1").Resize(1, 4).Value = Array("AA", "BB", "CC", "DD")
Range("A1").AutoFilter Field:=4, Criteria1:="Delete"
With ActiveSheet.AutoFilter.Range
.Columns(1).SpecialCells(xlVisible).EntireRow.Dele te
End With
ActiveSheet.AutoFilterMode = False
Columns(4).Delete

End Sub

--
Regards,
Tom Ogilvy





"Gabriel" wrote in message
om...
Hi,

I'm thinking to write a vba code in excel that filters a table based
on a precise criteria. For instance, I have a Table on Sheet1 that
looks like this:

a1 b1 c1
a2 b2 c2
a3 b3 c3

The macro should delete those rows that do not contain either a2 (on
column1) or b3 (on column2). In this example, the first row will be
the only one left.
Can anyone help ?

Thank you
Gabriel





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
Filter Date Code for macro mulehead Excel Discussion (Misc queries) 2 September 22nd 07 01:13 AM
Code to replace Advanced Filter RobN[_2_] Excel Discussion (Misc queries) 4 June 14th 07 12:31 PM
Filter isn't working with a VBA Code [email protected] Excel Worksheet Functions 8 June 15th 06 01:20 PM
Apply more than one filter in macro/vb code Stepnen Excel Programming 1 February 19th 04 07:49 PM
Is it posible to set up some code to Filter Worksheet on Colours Frederick Excel Programming 4 November 23rd 03 06:52 PM


All times are GMT +1. The time now is 02:58 PM.

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

About Us

"It's about Microsoft Excel"