Advanced filtering based off of another file Vlookup

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
799
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello - I have posted before but I am not getting any hits. so going to keep my problem brief and generic. Hopefully someone could lead me to an answer or refer me to a way to get there.

Existing Code: Will open another worksheet (CSV) and covert it to a format I need to upload. The data within that file has unique characters in column A. In my worksheet where I execute the code from has those same unique values in Column B but also has them grouped by Column A. And what I am looking to do is list out all of the Column A's I want to see in that upload.

Example below: (i only want my upload to show 10 records)

Book2
ABCD
1TypeUnique TagFor Export
2AAABC1AA
3AABBB1CC
4AACCC1EE
5AADDD1
6BBACD1
7BBADD1
8BBAFF1
9BBAGG1
10CCLLL1
11CCLLL2
12CCLLL3
13CCYYY4
14DDUUU8
15DDPPP0
16DDMNM1
17EEAFD1
18EEDF21
Sheet2
 
Yes - thank you
here is the upload column B2 is where filtering criteria is

Book2
ABCDEFGHIJKLMNOPQRSTUV
11aSymbol2a3a4a5a6a7a8a9a10a11a12a13a14a15a16a17a18a19a20a21a
2ABC1
3BBB1
4CCC1
5DDD1
6ACD1
7ADD1
8AFF1
9AGG1
10LLL1
11LLL2
12LLL3
13YYY4
14UUU8
15PPP0
16MNM1
17AFD1
18DF21
Converted
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
I had the 2 files the other way round ???
ie the converted data was starting at row 13 etc, etc
 
Upvote 0
Let's try again :oops:
- replace Execute with correct sheet name
- temp sheet deletion is commented out at the moment so that you can look at it if anything looks wrong in the results

VBA Code:
Sub AdvFilter()
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, ws4 As Worksheet
    Dim expArr As Variant, tagArr As Variant
    Dim coll As New Collection, App As Application
    Dim a As Long, b As Long, c As Long
    Dim dataRng As Range
    Set ws1 = Sheets("Execute")     'MUST AMEND SHEET NAME
    Set ws2 = Sheets("correct upload")
    Set dataRng = ws2.Range("A1").CurrentRegion
    Set App = Excel.Application
    App.ScreenUpdating = False: App.Calculation = xlCalculationManual
'create arrays of values
    expArr = ws1.Range("X13", ws1.Range("X" & ws1.Rows.Count).End(xlUp).Offset(1)).Value
    tagArr = ws1.Range("A13", ws1.Range("A" & ws1.Rows.Count).End(xlUp)).Resize(, 5).Value
'create filter list
    On Error Resume Next
    For a = 1 To UBound(tagArr)
        For b = 1 To UBound(expArr)
            If tagArr(a, 1) = expArr(b, 1) Then coll.Add CStr(tagArr(a, 5)), CStr(tagArr(a, 5))
        Next b
    Next a
    On Error GoTo 0
    c = coll.Count
'create temp sheet
    Set ws3 = ws2.Parent.Worksheets.Add(after:=ws2)
'add criteria
    ws3.Rows(1).Value = ws2.Rows(1).Value
    For a = 1 To c
        ws3.Cells(a + 1, 2) = coll(a)
    Next a
'add data and filter
    dataRng.Copy ws3.Cells(c + 3, 1)
    Set dataRng = ws3.Cells(c + 3, 1).CurrentRegion
    dataRng.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=ws3.Range("B1").Resize(c + 1), Unique:=False
'copy visible cells to results sheet
    Set ws4 = Sheets.Add(after:=ws2)
    dataRng.SpecialCells(xlCellTypeVisible).Copy ws4.Cells(1, 1)
    App.DisplayAlerts = False
    'ws3.Delete
    App.DisplayAlerts = True
    App.Calculation = xlCalculationAutomatic: App.ScreenUpdating = True
End Sub

Assuming that above works :unsure: we can then add a few lines to dump the results to another workbook

Exporting Results Sheet to another workbook
Required: Results Sheet exported as CSV and each CSV "goes to its own folder"
Q1 is this correct or do you place several files from the same upload in the same folder?

Q2 Do you want the code to allow you to ...
- browse to the correct main folder (Q3 is there a constant we can use here to get it to default to the correct main folder without browsing?)
- create a new subfolder (Q4 how is subfolder name determined?)
- export results sheet as csv with correct name to that folder (Q5 how is workbook name determined?)

Hopefully this makes it clear what I am referring to ..
Rich (BB code):
sMyFile = "path to correct main folder\" & "own subfolder name\" & "workbook name" & Format(Now, "MMDDYY") & ".csv"
 
Upvote 0
Let's try again :oops:
- replace Execute with correct sheet name
- temp sheet deletion is commented out at the moment so that you can look at it if anything looks wrong in the results

VBA Code:
Sub AdvFilter()
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, ws4 As Worksheet
    Dim expArr As Variant, tagArr As Variant
    Dim coll As New Collection, App As Application
    Dim a As Long, b As Long, c As Long
    Dim dataRng As Range
    Set ws1 = Sheets("Execute")     'MUST AMEND SHEET NAME
    Set ws2 = Sheets("correct upload")
    Set dataRng = ws2.Range("A1").CurrentRegion
    Set App = Excel.Application
    App.ScreenUpdating = False: App.Calculation = xlCalculationManual
'create arrays of values
    expArr = ws1.Range("X13", ws1.Range("X" & ws1.Rows.Count).End(xlUp).Offset(1)).Value
    tagArr = ws1.Range("A13", ws1.Range("A" & ws1.Rows.Count).End(xlUp)).Resize(, 5).Value
'create filter list
    On Error Resume Next
    For a = 1 To UBound(tagArr)
        For b = 1 To UBound(expArr)
            If tagArr(a, 1) = expArr(b, 1) Then coll.Add CStr(tagArr(a, 5)), CStr(tagArr(a, 5))
        Next b
    Next a
    On Error GoTo 0
    c = coll.Count
'create temp sheet
    Set ws3 = ws2.Parent.Worksheets.Add(after:=ws2)
'add criteria
    ws3.Rows(1).Value = ws2.Rows(1).Value
    For a = 1 To c
        ws3.Cells(a + 1, 2) = coll(a)
    Next a
'add data and filter
    dataRng.Copy ws3.Cells(c + 3, 1)
    Set dataRng = ws3.Cells(c + 3, 1).CurrentRegion
    dataRng.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=ws3.Range("B1").Resize(c + 1), Unique:=False
'copy visible cells to results sheet
    Set ws4 = Sheets.Add(after:=ws2)
    dataRng.SpecialCells(xlCellTypeVisible).Copy ws4.Cells(1, 1)
    App.DisplayAlerts = False
    'ws3.Delete
    App.DisplayAlerts = True
    App.Calculation = xlCalculationAutomatic: App.ScreenUpdating = True
End Sub

Assuming that above works :unsure: we can then add a few lines to dump the results to another workbook

Exporting Results Sheet to another workbook
Required: Results Sheet exported as CSV and each CSV "goes to its own folder"
Q1 is this correct or do you place several files from the same upload in the same folder?

Q2 Do you want the code to allow you to ...
- browse to the correct main folder (Q3 is there a constant we can use here to get it to default to the correct main folder without browsing?)
- create a new subfolder (Q4 how is subfolder name determined?)
- export results sheet as csv with correct name to that folder (Q5 how is workbook name determined?)

Hopefully this makes it clear what I am referring to ..
Rich (BB code):
sMyFile = "path to correct main folder\" & "own subfolder name\" & "workbook name" & Format(Now, "MMDDYY") & ".csv"
Ok tested the code two points:
1) it creates 2 new sheets? 1 of which looks correct and the other has duplicated data
2) it only brings over the symbol but all the other data isnt coming with it. for example although for filtering i didnt think it was needed but the other cells will have data in the upload i still require for the symbol.

Q1 - No they will all go to same folder every time but need to be saved in a structured format that cannot change
Q3 - Yes
Q4 - sub folder name is fixed each time wont ever change
Q5 - needs to be saved in a structured format that cannot change ever
 
Upvote 0
Ok tested the code two points:
1) it creates 2 new sheets? 1 of which looks correct and the other has duplicated data
2) it only brings over the symbol but all the other data isnt coming with it. for example although for filtering i didnt think it was needed but the other cells will have data in the upload i still require for the symbol.

Q1 - No they will all go to same folder every time but need to be saved in a structured format that cannot change
Q3 - Yes
Q4 - sub folder name is fixed each time wont ever change
Q5 - needs to be saved in a structured format that cannot change ever
my #2 i think is false i just retested and it does copy over
 
Upvote 0
Does the first of the sheets return expected values?
 
Upvote 0
It is not wonky!
It is an intermediate (workings) sheet
(See top of post#24)
Simply remove the apostrophe before
ws3.Delete
 
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,834
Members
449,471
Latest member
lachbee

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