VBA to find a value in multiple columns

Neoptolemos

New Member
Joined
Feb 28, 2011
Messages
4
I have an Excel file which has 9 columns, which all have some values in them, let's say A through G. The cells can also have multiple values in them (i.e. A, B, G).

I need some way to sort this file for all rows that have a particular letter in them, preferably using a textbox of some kind.

I've tried to do it with helper columns, but this gets really unwieldy because I need to create new columns for every value. The intended users of this file have even less Excel knowledge than I do, so preferably it should be very simple to operate.

I am using Excel 2007, but if this function is available in Excel 2010 I can upgrade.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Welcome to the board

Try:
Code:
Sub FindMe()
Dim searchitem As String
Dim i As Long, j As Long
On Error Resume Next
searchitem = InputBox("Enter your search item")
If searchitem = "" Then Exit Sub
For i = 1 To 9
    For j = 1 To Cells(Rows.Count, i).End(xlUp).Row
        If InStr(Cells(j, i), searchitem) <> 0 Then Cells(j, 10) = "Exists"
    Next j
Next i
ActiveSheet.AutoFilterMode = False
With Range(Cells(1, 1), Cells(1, 10))
    .AutoFilter
    .AutoFilter field:=10, Criteria1:="Exists"
End With  
End Sub
 
Upvote 0
Thanks JackDanIce, but I don't think that one quite works. I've uploaded this example to show you what I have right now. The example only includes 3 columns to be searched and values of A through C to keep things simple, but the textbox only applies to one column and I have no idea how to make it search columns D through F (OR, not AND).
 
Last edited:
Upvote 0
I can't seem to edit my post anymore, but by saying "[doesn't] quite work" I meant that while it works perfectly, I would prefer the script to filter out those rows not matching the request rather than creating an 'exists column'. The reason for this is that the database will be rather large, and to have an 'exists column' at column Z or something is not very practical.
 
Upvote 0
You could change the
Code:
Cells(j, 10) = "Exists"
to
Code:
cells(j, activesheet.usedrange.columns.count+1) = "Exists"
and
Code:
.autofilter field:=activesheet.usedrange.columns.count+1

That way it'd always put it in a new column at edge of your sheet. Food for thought.
 
Upvote 0

Forum statistics

Threads
1,224,508
Messages
6,179,188
Members
452,893
Latest member
denay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top