filter data in the same column by array

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hello
i need fixing my code to make filter data based on the same column it supposes bring data after write in four cells e3,f3,g3,h3 i try do that by array but not work


VBA Code:
Sub bring_customers()

       Dim CustID As String: CustID = sheet2.[e3].Value
       Dim CustID1 As String: CustID1 = sheet2.[f3].Value
       Dim CustID2 As String: CustID2 = sheet2.[g3].Value
       Dim CustID3 As String: CustID3 = sheet2.[h3].Value
        Dim FromDt As Long: FromDt = sheet2.[d3].Value
        Dim ToDt As Long: ToDt = sheet2.[c3].Value
     Set ar = Array(CustID, CustID1, CustID2, CustID3)
Application.ScreenUpdating = False
      
        sheet2.[A5].CurrentRegion.Offset(1).Clear
      
        With sheet1.[A2].CurrentRegion
        
                .AutoFilter 3, ar
                .AutoFilter 2, ">=" & FromDt, xlAnd, "<=" & ToDt
                .Offset(1).EntireRow.Copy sheet2.Range("A" & Rows.Count).End(3)(2)
                .AutoFilter
        End With
      
Application.ScreenUpdating = True

End Sub
thanks
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try this:

VBA Code:
Sub bring_customers()
  Dim ar As Range
  Dim FromDt As Long, ToDt As Long
  
  Application.ScreenUpdating = False
  FromDt = Sheet2.[D3].Value
  ToDt = Sheet2.[C3].Value
  Set ar = Sheet2.Range("E3:H3")
  Sheet2.Rows("6:" & Rows.Count).ClearContents
  
  With Sheet1.[A2].CurrentRegion
    .AutoFilter 3, ar.Value, xlFilterValues
    .AutoFilter 2, ">=" & FromDt, xlAnd, "<=" & ToDt
    .Offset(1).EntireRow.Copy Sheet2.Range("A6")
    .AutoFilter
  End With
  
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
hi, dante it works but i try to make code copy with headers from sheet1 begins row1 i amended this but the problem repeat the row of header twice from this
VBA Code:
    .Offset(1).EntireRow.Copy Sheet2.Range("A6")
to this
Code:
    .Offset(0).EntireRow.Copy Sheet2.Range("A6")
 
Upvote 0
In wich row do you have the headers?

Try this:

.EntireRow.Copy Sheet2.Range("A6")
 
Upvote 0
Im glad to help you, thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,586
Messages
6,120,402
Members
448,958
Latest member
Hat4Life

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