VBA Filter from another Tab based on cell value

garypea123

Board Regular
Joined
Mar 16, 2020
Messages
221
Office Version
  1. 365
Platform
  1. Windows
Good Morning All,

I am looking for a VBA which will look at the result from D3 (Table Tab) and then filter / copy the corresponding name from E2 onwards (Data Tab) and copy all the applicable data (A:L) under this name and paste into C10 (Table Tab)

For info if the word All is in the managers tab it should copy all data.

Thanks
Gary

Table Tab.JPG
Data Tab.JPG
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hello Gary,

See if this heads you in the right direction:-

VBA Code:
Option Explicit
Sub Test()

        Dim wsTT As Worksheet: Set wsTT = Sheets("Table Tab")
        Dim wsDT As Worksheet: Set wsDT = Sheets("Data Tab")
        Dim NmSearch As String: NmSearch = wsTT.[D3].Value

Application.ScreenUpdating = False
        
        wsTT.[C10].CurrentRegion.Clear
        
        With wsDT.Range("E2", wsDT.Range("E" & wsDT.Rows.Count).End(xlUp))
                .AutoFilter 1, NmSearch
                .Offset(1, -4).Resize(, 12).Copy wsTT.[C10]
                .AutoFilter
        End With

Application.ScreenUpdating = True

End Sub

I'm not sure if you are supposed have headings in the Table Tab starting in C9 or if you just want the relevant rows of data to just start in C10 and then clear the data when the code is next run (which is what the code above does).

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Hi, a VBA demonstration for starters :​
VBA Code:
Sub Demo1()
          Const S = "Table"
         Sheets(S).[C10].CurrentRegion.Clear
    With Sheets("Data")
        .[Z3].Formula = "=E3=""" & Sheets(S).[D3].Text & """"
        .[A1].CurrentRegion.Rows("2:" & .[A1].CurrentRegion.Rows.Count).AdvancedFilter 2, .[Z2:Z3], Sheets(S).[C10]
        .[Z3].Clear
    End With
End Sub
 
Upvote 0
As you have xl365, you could also use a formula
Excel Formula:
=FILTER(Sheet1!A3:L6,IF(D3="All",ROW(Sheet1!E3:E6)^0,Sheet1!E3:E6=D3))
 
Upvote 0
My demonstration revamped if cell D3 contains "all" :​
VBA Code:
Sub Demo1r()
   Const S = "Table"
     Dim T$
         T = Sheets(S).[D3].Text:  If UCase(T) = "ALL" Then T = "*"
         Sheets(S).[C10].CurrentRegion.Clear
    With Sheets("Data")
        .[Z2:Z3].Value2 = Evaluate("{""" & .[E2].Text & """;""" & T & """}")
        .[A1].CurrentRegion.Rows("2:" & .[A1].CurrentRegion.Rows.Count).AdvancedFilter 2, .[Z2:Z3], Sheets(S).[C10]
        .[Z2:Z3].Clear
    End With
End Sub
 
Upvote 0
Hello Gary,

Here's an update to allow for the criteria "All":
VBA Code:
Option Explicit
Sub Test()

        Dim wsTT As Worksheet: Set wsTT = Sheets("Table Tab")
        Dim wsDT As Worksheet: Set wsDT = Sheets("Data Tab")
        Dim NmSearch As String: NmSearch = wsTT.[D3].Value

Application.ScreenUpdating = False
        
        wsTT.[C10].CurrentRegion.Clear
        
        If NmSearch = "All" Then
                wsDT.Range("E3", wsDT.Range("L" & wsDT.Rows.Count).End(xlUp)).Copy wsTT.[C10]
                Else
                With wsDT.Range("E2", wsDT.Range("E" & wsDT.Rows.Count).End(xlUp))
                        .AutoFilter 1, NmSearch
                        .Offset(1, -4).Resize(, 12).Copy wsTT.[C10]
                        .AutoFilter
                End With
        End If
        
Application.ScreenUpdating = True

End Sub

Cheerio,
vcoolio.
 
Upvote 0
Hello Gary,

Here's an update to allow for the criteria "All":
VBA Code:
Option Explicit
Sub Test()

        Dim wsTT As Worksheet: Set wsTT = Sheets("Table Tab")
        Dim wsDT As Worksheet: Set wsDT = Sheets("Data Tab")
        Dim NmSearch As String: NmSearch = wsTT.[D3].Value

Application.ScreenUpdating = False
       
        wsTT.[C10].CurrentRegion.Clear
       
        If NmSearch = "All" Then
                wsDT.Range("E3", wsDT.Range("L" & wsDT.Rows.Count).End(xlUp)).Copy wsTT.[C10]
                Else
                With wsDT.Range("E2", wsDT.Range("E" & wsDT.Rows.Count).End(xlUp))
                        .AutoFilter 1, NmSearch
                        .Offset(1, -4).Resize(, 12).Copy wsTT.[C10]
                        .AutoFilter
                End With
        End If
       
Application.ScreenUpdating = True

End Sub

Cheerio,
vcoolio.
This appears to work quite well, but why does it miss rows A:D when using the all function?
 
Upvote 0
As you have xl365, you could also use a formula
Excel Formula:
=FILTER(Sheet1!A3:L6,IF(D3="All",ROW(Sheet1!E3:E6)^0,Sheet1!E3:E6=D3))
I like this and did not realise the new version could do this. My only concern is if others are using an old version this would cause an issue.
 
Upvote 0
Yes, that only works with 365 or online versions.
 
Upvote 0
This appears to work quite well, but why does it miss rows A:D when using the all function?
Hello Gary,

I've just seen my typo.

In this line:-

VBA Code:
wsDT.Range("E3", wsDT.Range("L" & wsDT.Rows.Count).End(xlUp)).Copy wsTT.[C10]

change the E3 to A3.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,836
Members
449,096
Latest member
Erald

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