advanced filter doesn't work for me

Hasson

Active Member
Joined
Apr 8, 2021
Messages
390
Office Version
  1. 2016
Platform
  1. Windows
hello
I have sheet2 contains values in column B and sheet1 contains data from A:F . I want using advanced filter . so the result in sheet2 based on column B should match with column A in sheet1 and brings filterd data in row8 .
VBA Code:
Sub AdvFilter()
  Dim rCrit As Range
  
 Dim ws As Worksheet
    Set ws = Sheets("sheet2")
  Set rCrit = ws.Range("B1:B2")
 With ThisWorkbook.Worksheets("sheet1")
  Range("A1", Range("E" & Rows.Count).End(xlUp)).Resize(, 5).AdvancedFilter 1, rCrit, Sheets("sheet2").Cells(8)
End With

End Sub
any chance to fixing this code
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi Hasson,

you talk about data in Column A to F (6 Columns) but only refer to 5 columns in the code. And the 1 you use is for xlFilterInPlace, the 2 is for xlFilterCopy.

You would need to adjust the ranges to suit:
VBA Code:
Sub AdvFilter()
Dim rCrit As Range
Dim ws As Worksheet

With ThisWorkbook
  Set ws = .Worksheets("sheet2")
  Set rCrit = ws.Range("B1:B2")
  With .Worksheets("sheet1")
    .Range("A1", .Range("E" & .Rows.Count).End(xlUp)).AdvancedFilter xlFilterCopy, rCrit, ws.Range("B8")
  End With
End With

Set rCrit = Nothing
Set ws = Nothing
End Sub
And to be honest: maybe use xl2BB for getting us an image if what you have and what you want. Because based on your descrition a different result may be wanted.

Cells(8) is H1 on my system, not Row 8.

Ciao,
Holger
 
Last edited:
Upvote 0
Your With block isn't currently doing anything. It should read:

VBA Code:
With ThisWorkbook.Worksheets("sheet1")
  .Range("A1", .Range("E" & .Rows.Count).End(xlUp)).Resize(, 5).AdvancedFilter 2, rCrit, Sheets("sheet2").Cells(8)
End With

Also, you had 1 as the action, which is to filter in place.
 
Upvote 0
@HaHoBe thanks but it copy all of data without filter based on B1:B2
maybe use xl2BB for getting us an image if what you have and what you want. Because based on your descrition a different result may be wanted.
REPORT.xlsm
ABCDEF
1ITEMIDQTY1QTY2QTY3BALANCE
21TT/W-1 MM CLA1 23M-1 IT500500
32QQW-2 TH NM-1 CLA2 VBG L CHI0
43QQW-3 CV CLA3 TAI70033733
54QQW-4 M*12.5 CLA4 TR20012188
65QQW-5 CLA5 EG300300
76MMR12/100 AS-1000/1 TMR120012
87QQW-6 M230 TU11
98QQW-7 S** CLA7 US140011399
109QQW-8 CLA8 UK0
1110QQW-9 CLA9 N BR160011599
1211QQW-10 BN CLA10 IT80000800
1312QQW-11 LVD CH900900
1413BB12 QQW-12 CLA12 JA100011989
sheet1
Cell Formulas
RangeFormula
F2:F14F2=C2+D2-E2


REPORT.xlsm
B
1TT/W-1 MM CLA1 23M-1 IT
2QQW-2 TH NM-1 CLA2 VBG L CHI
sheet2




result
REPORT.xlsm
ABCDEF
1TT/W-1 MM CLA1 23M-1 IT
2QQW-2 TH NM-1 CLA2 VBG L CHI
3
4
5
6
7
8ITEMIDQTY1QTY2QTY3BALANCE
91TT/W-1 MM CLA1 23M-1 IT500500
102QQW-2 TH NM-1 CLA2 VBG L CHI0
sheet2
Cell Formulas
RangeFormula
F9:F10F9=C9+D9-E9
 
Upvote 0
B1 needs to be a column header, not a filter value. So you'll need to move those two down, add the column header "ID" to B1, then expand the criteria range to use B1:B3.
 
Upvote 0
@RoryA you wanna to say the advanced filter start from the header?
actually this is the first time using advanced filter I no know the rules , sorry !
 
Upvote 0
The first cell in the criteria range should be the header of the column you are trying to filter by - ID in this case.
 
Upvote 0
Hi Hasson,

sheet2 needs to look like Rory stated
Mappe1
ABCDEF
1ID
2TT/W-1 MM CLA1 23M-1 IT
3QQW-2 TH NM-1 CLA2 VBG L CHI
4
5
6
7
8ITEMIDQTY1QTY2QTY3BALANCE
91TT/W-1 MM CLA1 23M-1 IT500500
102QQW-2 TH NM-1 CLA2 VBG L CHI0
Sheet2


And the code should be modified to this
Code:
Sub AdvFilter()
Dim rCrit As Range
Dim ws As Worksheet

With ThisWorkbook
  Set ws = .Worksheets("sheet2")
  Set rCrit = ws.Range("B1:B3")
  With .Worksheets("sheet1")
    .Range("A1", .Cells(.Range("A" & .Rows.Count).End(xlUp).Row, "F")).AdvancedFilter xlFilterCopy, rCrit, ws.Range("A8")
  End With
End With

Set rCrit = Nothing
Set ws = Nothing
End Sub{/code]
CIao,
Holger
 
Upvote 0
Solution
@HaHoBe thanks but it shows problem if I change the items to filter another doesn't replace it keeping old data, how can I fix it,please?
 
Upvote 0
Hi Hasson,

the use of Advanced Filter should always clear the data and add the newly imported one. The data will stay on the sheet as long as you run the macro again or you delete the output. One way to go may be use this code behind the worksheet:
Code:
Private Sub Worksheet_Deactivate()
Range("A8").CurrentRegion.ClearContents
End Sub
which will delete the result when you leave the sheet (activate another sheet).

Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,215,517
Messages
6,125,288
Members
449,218
Latest member
Excel Master

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