Advanced filtering based off of another file Vlookup

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
803
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
 
Am so sorry I left off a BIG piece and could simplify this a lot. My code actually will copy the upload file into the workbook that has the code into it and place it in its own sheet then does the formatting. When done it exports it out to its own workbook. I totally forgot that

Q1 - it does not ensure but the data should not have empty columns/rows part of the formatting does that removes empty rows
Q2 - No nothing manual after the upload is modified
Q3 - Yes it has a variable where it finds the partial format and date meets today in order to open the upload if not met it just kicks out a dialog box.
Q4 - In its own workbook with a static naming i can define when everything is all said and done
Q5 - so the column A and B ends up being static just a different location see below:
Column A (Title= Account) starts on A13
Column B (Title = Symbol) starts on E13
And in the upload the unique tag or symbol is in column B
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I think we are almost there .. :unsure:

Yes - it does make it easier that the converted data sheet is in the same workbook
Q1 Is the same sheet used every time for converted data or is a new one generated by the code every time?

Sorry I am confused (not sure if Account = Type and Symbol = Tag or if it's the other way round!)
Q2 Is this the correct match?
- Column A (Type) matches column A (Account) in data
- Column B (Tag) matches column E (Symbol) in data

Each result will be placed its own workbook with a static naming ...
Q3 Are they all saved to the same folder?
Q4 Do you want ...
(a) to manually name each file OR
(b) VBA to autogenerate the whole name OR
(c) autogenerate component + manual component
 
Upvote 0
I think we are almost there .. :unsure:

Yes - it does make it easier that the converted data sheet is in the same workbook
Q1 Is the same sheet used every time for converted data or is a new one generated by the code every time?

Sorry I am confused (not sure if Account = Type and Symbol = Tag or if it's the other way round!)
Q2 Is this the correct match?
- Column A (Type) matches column A (Account) in data
- Column B (Tag) matches column E (Symbol) in data

Each result will be placed its own workbook with a static naming ...
Q3 Are they all saved to the same folder?
Q4 Do you want ...
(a) to manually name each file OR
(b) VBA to autogenerate the whole name OR
(c) autogenerate component + manual component
Q1 - Yes I have it in its own tab called "correct upload"
Q2 - that is correct account is type and unique tag is symbol
Q3 - No unfortunately the upload goes to its own folder
Q4 - Right now I have this that i believe is equivalent to your (C). but am not opposed of doing (b)
VBA Code:
Sub GetNameAndSaveAsCSV2()

    Dim oWb         As Workbook
    Dim sMyFile     As String
    Dim sSavedFile  As String

    sMyFile = "network path\" & "XXXX" & Format(Now, "MMDDYY") & ".csv"  ' <<< change as required
 
    Set oWb = ActiveWorkbook                                    ' <<< change as required

    ' return with drive:\folder\filename.ext  of saved file
    sSavedFile = FileSaveAs(oWb, sMyFile)

    Set oWb = Nothing
End Sub
 
Upvote 0
Let's get it working within the one workbook first (before exporting Results sheet to another workbook - that's the easy bit!)
- I have tried to take account of everything you have told me

A. Latest code assumes that there is a sheet looking exactly like the one I named Execute (with columns A,B & D)
- if that is not the case then go no further, and revert back to me with some further details

B. Replacement code is below. I do not know the correct name for sheet containing columns A,B & D - amend this line in the code
Rich (BB code):
Set ws1 = Sheets("Execute")

C. The macro will stop after writing the data range to the immediate window using Debug.Print
Display that window with {CTRL} g when in VBA editor
Check the data range address against the actual data in sheet correct upload

D. If the range address is correct ...
... remove these 2 lines and run the code again
VBA Code:
Debug.Print "Is this the correct data range?", dataRng.Address(0, 0)
End

E. Both temporary and results sheets should appear
(they are now named to make life easier)

F. Let me know if there are any problems before you try amending the code at this stage

G. What is the correct name for sheet ws1?
- I need to amend my version too!

Replacement procedure
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, lastR As Long, lastC As Long
    Dim dataRng As Range
    Const z As Long = 12                        'rows to ignore
    Set ws1 = Sheets("Execute")     'MUST AMEND SHEET NAME
    Set ws2 = Sheets("correct upload")
    lastR = ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row - z
    lastC = ws2.Cells(13, ws2.Columns.Count).End(xlToLeft).Column
    Set dataRng = ws2.Cells.Resize(lastR, lastC).Offset(z)
Debug.Print "Is this the correct data range?", dataRng.Address(0, 0)  'delete if first test successful
End   'delete if first test successful
    Set App = Excel.Application
    App.ScreenUpdating = False: App.Calculation = xlCalculationManual
'create arrays of values
    expArr = ws1.Range("D2", ws1.Range("D" & ws1.Rows.Count).End(xlUp).Offset(2)).Value
    tagArr = ws1.Range("A2", ws1.Range("A" & ws1.Rows.Count).End(xlUp)).Resize(, 2).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, 2)), CStr(tagArr(a, 2))
        Next b
    Next a
    On Error GoTo 0
    c = coll.Count
'create temp sheet
    Set ws3 = ws2.Parent.Worksheets.Add(after:=ws2)
    ws3.Name = "temp" & Format(Now, "_mmdd_hh.mm")
'add criteria
    ws3.Cells(1, 1) = ws2.Cells(z + 1, 1)
    For a = 1 To c
        ws3.Cells(a + 1, 1) = 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("A1").Resize(c + 1), Unique:=False
'copy visible cells to results sheet
    Set ws4 = Sheets.Add(after:=ws2)
    ws4.Name = "Result" & Format(Now, "_mmdd_hh.mm")
    dataRng.SpecialCells(xlCellTypeVisible).Copy ws4.Cells(1, 1)
    App.DisplayAlerts = False
    'ws3.Delete    '(DO NOT REMOVE THE LEADING APOSTROPHE UNTIL WE KNOW ALL IS WELL!)
    App.DisplayAlerts = True
    App.Calculation = xlCalculationAutomatic: App.ScreenUpdating = True
End Sub
 
Upvote 0
Let's get it working within the one workbook first (before exporting Results sheet to another workbook - that's the easy bit!)
- I have tried to take account of everything you have told me

A. Latest code assumes that there is a sheet looking exactly like the one I named Execute (with columns A,B & D)
- if that is not the case then go no further, and revert back to me with some further details

B. Replacement code is below. I do not know the correct name for sheet containing columns A,B & D - amend this line in the code
Rich (BB code):
Set ws1 = Sheets("Execute")

C. The macro will stop after writing the data range to the immediate window using Debug.Print
Display that window with {CTRL} g when in VBA editor
Check the data range address against the actual data in sheet correct upload

D. If the range address is correct ...
... remove these 2 lines and run the code again
VBA Code:
Debug.Print "Is this the correct data range?", dataRng.Address(0, 0)
End

E. Both temporary and results sheets should appear
(they are now named to make life easier)

F. Let me know if there are any problems before you try amending the code at this stage

G. What is the correct name for sheet ws1?
- I need to amend my version too!

Replacement procedure
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, lastR As Long, lastC As Long
    Dim dataRng As Range
    Const z As Long = 12                        'rows to ignore
    Set ws1 = Sheets("Execute")     'MUST AMEND SHEET NAME
    Set ws2 = Sheets("correct upload")
    lastR = ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row - z
    lastC = ws2.Cells(13, ws2.Columns.Count).End(xlToLeft).Column
    Set dataRng = ws2.Cells.Resize(lastR, lastC).Offset(z)
Debug.Print "Is this the correct data range?", dataRng.Address(0, 0)  'delete if first test successful
End   'delete if first test successful
    Set App = Excel.Application
    App.ScreenUpdating = False: App.Calculation = xlCalculationManual
'create arrays of values
    expArr = ws1.Range("D2", ws1.Range("D" & ws1.Rows.Count).End(xlUp).Offset(2)).Value
    tagArr = ws1.Range("A2", ws1.Range("A" & ws1.Rows.Count).End(xlUp)).Resize(, 2).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, 2)), CStr(tagArr(a, 2))
        Next b
    Next a
    On Error GoTo 0
    c = coll.Count
'create temp sheet
    Set ws3 = ws2.Parent.Worksheets.Add(after:=ws2)
    ws3.Name = "temp" & Format(Now, "_mmdd_hh.mm")
'add criteria
    ws3.Cells(1, 1) = ws2.Cells(z + 1, 1)
    For a = 1 To c
        ws3.Cells(a + 1, 1) = 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("A1").Resize(c + 1), Unique:=False
'copy visible cells to results sheet
    Set ws4 = Sheets.Add(after:=ws2)
    ws4.Name = "Result" & Format(Now, "_mmdd_hh.mm")
    dataRng.SpecialCells(xlCellTypeVisible).Copy ws4.Cells(1, 1)
    App.DisplayAlerts = False
    'ws3.Delete    '(DO NOT REMOVE THE LEADING APOSTROPHE UNTIL WE KNOW ALL IS WELL!)
    App.DisplayAlerts = True
    App.Calculation = xlCalculationAutomatic: App.ScreenUpdating = True
End Sub
Okay so for (A) we need to stop then. currently the range is from A13-V13 then could go down infinite (where I want to use X13 and down to where we define what to execute). The data we care about for the relationship starts on A13 for account and E13 for symbol (what will be on the upload). Those header names end up being different in the upload not sure if that matters or not. but they will always be in the same column in the upload if that helps at all.
 
Upvote 0
Sorry I am now totally confused
Please use xl2bb to upload something I can look at that shows me the real data in the 2 sheets and then we can take it from there
 
Upvote 0
Sorry I am now totally confused
Please use xl2bb to upload something I can look at that shows me the real data in the 2 sheets and then we can take it from there
Sure see below for how the sheet with the relationship is comprised. the data could go down infinitely. and the header names I just made up for the data not relevant

Book2
ABCDEFGHIJKLMNOPQRSTUVWX
1
2
3
4
5
6
7
8
9
10
11
12Account1a2aSymbol3a4a5a6a7a8a9a10a11adiffdiffdiffdiff12aFor Export
13AAABC1BB
14AABBB1
15AACCC1
16AADDD1
17BBACD1
18BBADD1
19BBAFF1
20BBAGG1
21CCLLL1
22CCLLL2
23CCLLL3
24CCYYY4
25DDUUU8
26DDPPP0
27DDMNM1
28EEAFD1
29EEDF21
Execute
 
Upvote 0
Yes, my understanding was wrong :oops:
Will post updated code tomorrow or Sunday
 
Upvote 0
Yes, my understanding was wrong :oops:
Will post updated code tomorrow or Sunday
Sorry that could have been my fault for not being clear in the beginning. Do you need the upload format as well? That one the symbol we need for filtering starts on B2.
 
Upvote 0

Forum statistics

Threads
1,216,484
Messages
6,130,936
Members
449,608
Latest member
jacobmudombe

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