Search function looking for value in multiple columns and filtering

sofiachr

Board Regular
Joined
Jan 15, 2013
Messages
98
Hi,

bild.PNG


I have this Excel sheet where I have my training exercises and I want to aply a search function since they are becoming many now. I have the muscle the exersice activates in column A and B and C since there are normaly multiple muscels activated. (In Swedish at picture, Muskel 1 and 2 and 3).

The other criterias, Kondition (condition) and Namn (name), works. But the muscel criteria (Muskel) need to be looking for the search value in both column A and B and C (from row 6 and down) since it could be in any of these columns.

I would like to create a macro I can link to the search button (Sök) to simplify daily use. And the end result should be a filtered list with those exercises that are activating the muscle I searched for.

Thanks!
Sincerely, Sofia
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
This should work for you
VBA Code:
Sub SearchMuscles()

    Dim fnd As Range, last_row As Long, current_row As Range
    Dim muscle As String
    
    With Sheets("Sheet1") '<--CHANGE "Sheet1" TO THE NAME OF YOUR WORKSHEET
        .Rows.EntireRow.Hidden = False
        
        muscle = .Range("A3").Value
        last_row = .Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
        For Each current_row In .Rows("6:" & last_row)
            With current_row
                Set fnd = Range(.Cells(, "A"), .Cells(, "C")).Find( _
                    What:=muscle, _
                    LookIn:=xlValues, _
                    SearchOrder:=xlByColumns)
                    
                If fnd Is Nothing Then
                    .EntireRow.Hidden = True
                End If
            End With
        Next current_row
    End With

End Sub
 
Upvote 0
This should work for you
VBA Code:
Sub SearchMuscles()

    Dim fnd As Range, last_row As Long, current_row As Range
    Dim muscle As String
   
    With Sheets("Sheet1") '<--CHANGE "Sheet1" TO THE NAME OF YOUR WORKSHEET
        .Rows.EntireRow.Hidden = False
       
        muscle = .Range("A3").Value
        last_row = .Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
       
        For Each current_row In .Rows("6:" & last_row)
            With current_row
                Set fnd = Range(.Cells(, "A"), .Cells(, "C")).Find( _
                    What:=muscle, _
                    LookIn:=xlValues, _
                    SearchOrder:=xlByColumns)
                   
                If fnd Is Nothing Then
                    .EntireRow.Hidden = True
                End If
            End With
        Next current_row
    End With

End Sub

Thanks! It works perfect. How do I make it work with the other two fields? The kondition (condition) and Namn (name). I could use a simple advanced filter for these before but it would be good to make these two solutions work in one click. So that these are considered when I press search (Sök) too.
 
Upvote 0
How do I make it work with the other two fields?

With advanced filter , but you must have more rows, check the following example.
The names of the headers in A2 through E2 must be equal to the names of the headers in A1 through E10

varios 08feb2020.xlsm
ABCDEF
1
2Muskel 1Muskel 2Muskel 3Kondition?Namn
3Rumpanej
4Rumpanej
5Rumpanej
6
7
8
9
10Muskel 1Muskel 2Muskel 3Kondition?NamnBesk
11MageNeja
12MageNejb
13RumpaNejc
14RumpaNejd
15RumpaNeje
16RumpaJaf
17RumpaJag
18Rumpah
sheet


The macro:

VBA Code:
Sub Macro3()
  Range("A10:F" &  Range("E" & Rows.Count).End(xlUp).Row).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("A2:E5")
End Sub
 
Upvote 0
Just a small adjustment
VBA Code:
Sub SearchMuscles()

    Dim mySheet As Worksheet
    Dim fnd As Range, last_row As Long, current_row As Range
    Dim muscle As String
  
    Set mySheet = Sheets("Sheet1") '<--CHANGE "Sheet1" TO THE NAME OF YOUR WORKSHEET
  
    With mySheet
        .Rows.EntireRow.Hidden = False
      
        muscle = .Range("A3").Value
        last_row = .Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
      
        For Each current_row In .Rows("6:" & last_row)
            With current_row
                Set fnd = Range(.Cells(, "A"), .Cells(, "C")).Find( _
                    What:=muscle, _
                    LookIn:=xlValues, _
                    SearchOrder:=xlByColumns)
                  
                If fnd Is Nothing Then
                    .EntireRow.Hidden = True
                Else
                    If mySheet.Range("B3") <> Empty Then
                        If .Cells(, "D") <> mySheet.Range("B3") Then
                            .EntireRow.Hidden = True
                        End If
                    End If
                   
                    If mySheet.Range("C3") <> Empty Then
                        If .Cells(, "E") <> mySheet.Range("C3") Then
                            .EntireRow.Hidden = True
                        End If
                    End If
                End If
            End With
        Next current_row
    End With

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,094
Latest member
teemeren

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