Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default using autofilter to search 2 columns

I have a bit of code which ive used to get autofilter to search a colum
of a bunch of tables for rows where the cell in that column 'contains
the text that the user has selected in a combobox, and then shows al
rows with this text.

What i am trying to do now is adapt this code so that i do the sam
thing except with TWO columns, and the text its searching for will b
what the user has typed into a textbox.

Ideally it will do this as the user types in the text, i.e. they type
6 and it lists all the relevant rows containing a 6 anywhere in eithe
of the 2 columns im searching in, then type 5 and it re-does the searc
to be anything with *65* in that column etc. This may be too much fo
excel to handle making it slow or something, so if this is a bad way t
do it then it would be nice to make it seatch when the user presses th
enter key after typing their text in the textbox.

below is the code ive got so far, it only searches column 2, but i nee
it to search column 3 as well. It also re-does the search every tim
they type or delete a character (because its in textbox change event)
but for some reason i cant get it to search for 'contains', it onl
seems to produce a result when an exact match is found rather tha
anything containing the typed text, dont know why.

Private Sub TextBox1_Change()
Dim i As Long

If Me.TextBox1.Text = "" Then
Me.AutoFilterMode = False
Else
Me.Range("mytables").AutoFilter 2, _
"=*" & Me.TextBox1.Text & "*", xlAnd, , False

With Me.Range("mytables")
For i = 1 To 8
If i < 2 Then
.AutoFilter i, , , , False
End If
Next i
End With
End If

End Sub

(the 'with' code just goes along and turns off the dropdowns on each o
the 8 columns, 'mytables' is the cells containing the 4 tables i
searching which are one above the other).

anyone help with this

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 184
Default using autofilter to search 2 columns

Hi,

you should add one column that combines the 2 other columns as you wish.
Then you can filter based on that new column.

arno


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default using autofilter to search 2 columns

cant do that they have to be 2 seperate columns and are change
regularly.

i just tried a plain autofilter from th data menu then went to custom
and for some reason autofilter is refusing to filter on that colum
properly. if i select contains 6, it comes up with no results when th
column contains dozens of 6's. if i try the same thing on the nex
column it works perfectly. the ONLY difference between the 2 column
is that the one that doesnt work only has numbers in it (which seem t
be right aligned even though theres no formatting on the cells) and th
column that works fine has letters in the cells and is left aligned
even though i havent formatted it to be. yet both are down a
"general" number format when going to cell properties so they should b
identical.

i just formatted the column that only contains numbers in the cells
and told it the format is 'text'. and its STILL not working

--
Message posted from http://www.ExcelForum.com

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 184
Default using autofilter to search 2 columns


"neowok " schrieb im Newsbeitrag
...
cant do that they have to be 2 seperate columns and are changed
regularly.


believe me, having a dummy-column with a formula combining the other 2
columns will solve all of your problems. you just have to make sure to copy
the formula far enaough. your filter does not work because your data is a
mixture of text and numbers.

arno


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default using autofilter to search 2 columns

one way, using a helper column:

Private Sub TextBox1_Change()
With Sheets("Sheet3")
.Range("I1").Value = TextBox1.Text
.Range("I2").Formula = _
"=AND(ISERR(SEARCH(I$1,B2)),ISERR(SEARCH(I$1,C2))) "
.Range("I2").AutoFill _
Destination:=.Range("I2:I" & .Range("B" & _
Rows.Count).End(xlUp).Row), _
Type:=xlFillDefault
Range("A1").AutoFilter _
Field:=9, _
Criteria1:="FALSE", _
VisibleDropDown:=False
.Columns(9).Hidden = True
End With
End Sub


In article ,
neowok wrote:

I have a bit of code which ive used to get autofilter to search a column
of a bunch of tables for rows where the cell in that column 'contains'
the text that the user has selected in a combobox, and then shows all
rows with this text.

What i am trying to do now is adapt this code so that i do the same
thing except with TWO columns, and the text its searching for will be
what the user has typed into a textbox.

Ideally it will do this as the user types in the text, i.e. they type a
6 and it lists all the relevant rows containing a 6 anywhere in either
of the 2 columns im searching in, then type 5 and it re-does the search
to be anything with *65* in that column etc. This may be too much for
excel to handle making it slow or something, so if this is a bad way to
do it then it would be nice to make it seatch when the user presses the
enter key after typing their text in the textbox.

below is the code ive got so far, it only searches column 2, but i need
it to search column 3 as well. It also re-does the search every time
they type or delete a character (because its in textbox change event),
but for some reason i cant get it to search for 'contains', it only
seems to produce a result when an exact match is found rather than
anything containing the typed text, dont know why.

Private Sub TextBox1_Change()
Dim i As Long

If Me.TextBox1.Text = "" Then
Me.AutoFilterMode = False
Else
Me.Range("mytables").AutoFilter 2, _
"=*" & Me.TextBox1.Text & "*", xlAnd, , False

With Me.Range("mytables")
For i = 1 To 8
If i < 2 Then
.AutoFilter i, , , , False
End If
Next i
End With
End If

End Sub

(the 'with' code just goes along and turns off the dropdowns on each of
the 8 columns, 'mytables' is the cells containing the 4 tables im
searching which are one above the other).

anyone help with this?


---
Message posted from http://www.ExcelForum.com/



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default using autofilter to search 2 columns

hmm i cant get that to work.

a sample of the tables im using i
http://www.darkcity.nildram.co.uk/listtest.xls

it isnt the case on that sample but sometimes there will be somethin
in the lul ref column AND the tl ref column (so it needs to list it i
either column contains the typed value)

--
Message posted from http://www.ExcelForum.com

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default using autofilter to search 2 columns

Autofilters don't work that way. Conditions applied on separate columns are
additive (Logical AND conditions).

If you have lul set for non blanks and tl set for non blanks, then you would
only see rows where both lul and tl were non blank.

You can get what you want using an Advanced filter, but you have to specify
the critieria in a range of cells (it doesn't have to be on the same sheet.)

If you want an example, contact me with your email address and I will set up
a sample on your file and mail it to you.



--
Regards,
Tom Ogilvy


"neowok " wrote in message
...
hmm i cant get that to work.

a sample of the tables im using is
http://www.darkcity.nildram.co.uk/listtest.xls

it isnt the case on that sample but sometimes there will be something
in the lul ref column AND the tl ref column (so it needs to list it if
either column contains the typed value).


---
Message posted from http://www.ExcelForum.com/



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
Autofilter on rows instead of columns Laurent Excel Worksheet Functions 6 May 21st 07 10:17 AM
autofilter on many columns inquirer Excel Discussion (Misc queries) 3 July 16th 06 04:32 AM
Windows search in Excel with autofilter Dave Z Excel Discussion (Misc queries) 1 January 21st 05 07:44 PM
VBA to set which columns in Autofilter miker1999[_10_] Excel Programming 2 February 15th 04 02:44 AM
Use sheet name as search string or autofilter criteria Magar Excel Programming 1 November 14th 03 01:06 PM


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