AdvancedFilter - filter a specific column with its value from another NOT adjacent column

96 views Asked by At

i already asked this question kind of on stackoverflow but i think i made myself indistinct. Below you can see the code. What I'm trying to do is to get the AdvancedFilter on Column C in addition to its pair in Column I and also just copy these unique values together to an adjacent range (A to C a new worksheet)

What i tried to do is to use set and declare "Discount" to a specific Range which gets filtered with & too. Important to know is that C:C gets splitted, so on the new sheet dws it needs column A and B,the variable Discounts therefore shall be in Cloumn C in dws.

Sub Unique_Values_Worksheet_Variables()
    
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim sws As Worksheet: Set sws = wb.Worksheets("export")
    Dim dws As Worksheet:
    Set dws = wb.Worksheets.Add(After:=wb.Sheets(wb.Sheets.Count))
    **Set Discounts = ActiveSheet.Range("I:I")**
    
    sws.Range("C:C" **& Discounts**).AdvancedFilter _
        Action:=xlFilterCopy, _
        CopyToRange:=dws, _
        Unique:=True

  dws.Columns("A:B").EntireColumn.AutoFit
    Dim rng As Range:
    Set rng = dws.Range("A1:B1", dws.Cells(dws.Rows.Count, 1).End(xlUp))
    rng.Borders(xlDiagonalDown).LineStyle = xlNone
    rng.HorizontalAlignment = xlCenter

What i would like to know is, if the & is the right approach and if its just a problem of the assigned Ranges itself or if is a general logic mistake.

For example

  • Do i have to set first the whole range where the filter shall work in? Set Area = ActiveSheet.Range("A:Z")?
  • Is the union method a better approach? (I already tried also this one)

I have to write it as an Makro because the Sheet where the filter works on shall work by just one button whereby the sheet itself is always different.

0

There are 0 answers