fixing code to advanced filter for two columns together

Ali M

Active Member
Joined
Oct 10, 2021
Messages
287
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hi
I try copying data from A:G if the columns C,F just contains values together . should ignore two cases if the column C,F contains zero together or columns D,E one of them contains values
this the code . currently copy whole data without exceptions
VBA Code:
Sub arrange()
    Dim r As Range
    Sheets("sh").Cells(1).CurrentRegion.Clear
    With ActiveSheet
        Set r = .Cells.SpecialCells(4).EntireColumn.Range("G1:G2")
        r(2).Formula = "=AND(n(C2)<>0,n(F2)<>0)"
        .Range("A1", .Range("A" & Rows.Count).End(xlUp)).Resize(, 7).AdvancedFilter 2, r, Sheets("sh").Cells(1)
        r.Clear
    End With
    With Sheets("sh").Cells(1).CurrentRegion
        If .Rows.Count > 1 Then
            .Offset(1).Resize(.Rows.Count - 1).Columns(1) = Evaluate("row(1:" & .Rows.Count - 1 & ")")
        End If
    End With
End Sub
I  hope  finding  help
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Are you able to get the Advanced Filter formula to work when you do it manually, and if so what is the full formula in your criteria cell ?
Your current formula does not cater for the or columns D,E one of them contains values. Is the test here for either of them being a numeric value and is 0 and acceptable value ?
 
Upvote 0
Hi Alex,

in reality there is no any formula in any cells . just using by code to do that .

yes should just search for numeric values for columns D,E with exclude cells contain 0 or empty cells for columns D,E
 
Upvote 0
I am unclear of what your final criteria are. Ideally you create the formula in Excel first and try to filter it manually to prove the formula is correct before trying to use in in VBA.
Assuming the logic is that C AND F must have a non-zero value in it AND EITHER D OR E have a non-zero value in it, the formula below should work.
Excel Formula:
=AND(AND(N(C2)<>0,N(F2)<>0),OR(N(D2)<>0,N(E2)<>0))

I have used that in the code below.
VBA Code:
Sub AdvancedFilterCopy()

    Dim srcSht As Worksheet, destSht As Worksheet
    Dim rngSrc As Range, rngDest As Range, rngCrit As Range
    Dim srcRowFirst As Long, srcRowLast As Long, srcColLast As Long
    Dim noOfCritCols As Long, noOfCritRows As Long
       
    Set srcSht = ActiveSheet                ' <--- Change this as required
    Set destSht = Worksheets("sh")          ' <--- Change this as required
    srcRowFirst = 1
    noOfCritCols = 1
    noOfCritRows = 2    ' Including Heading
        
    With srcSht
        srcRowLast = srcSht.Cells(Rows.Count, "G").End(xlUp).Row
        srcColLast = srcSht.Cells(srcRowFirst, Columns.Count).End(xlToLeft).Column
        Set rngSrc = .Range(.Cells(srcRowFirst, "A"), .Cells(srcRowLast, srcColLast))
        ' Temporary range for criteria
        .Columns(srcColLast + 2).Resize(, noOfCritCols).EntireColumn.Insert
        Set rngCrit = .Cells(srcRowFirst, srcColLast + 2).Resize(noOfCritRows, noOfCritCols)
    End With
    
    With destSht
        .Cells.Clear
        Set rngDest = .Range("A1")
    End With
    
    ' Set Criteria
    ' Column C criteria
    rngCrit.Cells(1, 1).Value = "Formula Criteria"                                      ' Optional temporary criteria headng
    rngCrit.Cells(2, 1).Value = "=AND(AND(N(C2)<>0,N(F2)<>0),OR(N(D2)<>0,N(E2)<>0))"    ' Filter formula
    
    ' Apply Filter and Copy to Destination
    rngSrc.AdvancedFilter xlFilterCopy, rngCrit, rngDest
    rngCrit.EntireColumn.Delete
    
    With rngDest
        .CurrentRegion.EntireColumn.AutoFit
    End With

End Sub
 
Upvote 0
what does mean this error?

This command requires at least two rows of source data. You cannot use the command on a selection in only one row. Try the following:

- If you're using an advanced filter, select a range of cells that contains at least two rows of data. Then click the Advanced Filter command again.
- or you're creating a PivotTable, type a cell reference or select a range that includes at least two rows of data

in this line
VBA Code:
 rngSrc.AdvancedFilter xlFilterCopy, rngCrit, rngDest
 
Upvote 0
Without you providing an XL2BB of your data I am guessing the information required by the code.
The code assumes:
• The activesheet is the sheet to which you are going to apply the filter
• using xlup on Column G will give you the last row of the area to be filters
• the headings on sheet to be filtered are on row 1
• I haven't checked this but ideally row 1 has a column heading for Columns A to G and beyond if there are more columns.
• That your output sheet name is "Sh" and that the output is to A1
 
Upvote 0
The code assumes:
• The activesheet is the sheet to which you are going to apply the filter
• using xlup on Column G will give you the last row of the area to be filters
• the headings on sheet to be filtered are on row 1
• I haven't checked this but ideally row 1 has a column heading for Columns A to G and beyond if there are more columns.
• That your output sheet name is "Sh" and that the output is to A1
that's right

Without you providing an XL2BB of your data I am guessing the information required by the code.
ok
should be based on column C,F
asd.xlsx
ABCDEFG
1DATEBRANDSTOCKBUYINGSELLINGBALANCENOTE
21/7/2022REF-BSTR-1002344463
31/8/2022REF-BSTR-10155154
41/9/2022REF-BSTR-10255
51/10/2022REF-BSTR-10377
61/11/2022REF-BSTR-10445660
71/12/2022REF-BSTR-1055757
81/13/2022REF-BSTR-10645144
91/14/2022REF-BSTR-10755451
101/15/2022REF-BSTR-10867166
111/16/2022REF-BSTR-10945243
121/17/2022REF-BSTR-1104444
131/18/2022REF-BSTR-1117777
141/19/2022REF-BSTR-1128585
151/20/2022REF-BSTR-11376670
161/21/2022REF-BSTR-11477
171/22/2022REF-BSTR-11577
main


result
asd.xlsx
ABCDEFG
1DATEBRANDSTOCKBUYINGSELLINGBALANCENOTE
21/7/2022REF-BSTR-1002344463
31/9/2022REF-BSTR-10255
41/10/2022REF-BSTR-10377
51/11/2022REF-BSTR-10445660
61/12/2022REF-BSTR-1055757
71/13/2022REF-BSTR-10645144
81/17/2022REF-BSTR-1104444
91/18/2022REF-BSTR-1117777
101/19/2022REF-BSTR-1128585
111/21/2022REF-BSTR-11477
SH
 
Upvote 0
Thank you for providing the XL2BB data and expected results.

So ok a couple of changes.
1) Based on your XL2BB we can not rely on column G for your last row so change this from G to A:
Rich (BB code):
    With srcSht
        srcRowLast = srcSht.Cells(Rows.Count, "G").End(xlUp).Row
        srcRowLast = srcSht.Cells(Rows.Count, "A").End(xlUp).Row

2) The Post 1 & 4 references to filtering on Column D & E appear to be irrelevant to change this line as well:
Rich (BB code):
    rngCrit.Cells(2, 1).Value = "=AND(AND(N(C2)<>0,N(F2)<>0),OR(N(D2)<>0,N(E2)<>0))"    ' Filter formula
    rngCrit.Cells(2, 1).Value = "=AND(N(C2)<>0,N(F2)<>0)"    ' Filter formula
 
Upvote 0
Solution

Forum statistics

Threads
1,215,036
Messages
6,122,796
Members
449,095
Latest member
m_smith_solihull

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