Please help with VBA code for filtering data?

msb1977

Board Regular
Joined
Apr 22, 2016
Messages
78
Hi all

The below code works, but the arrays for filtering data will only accept two variables to filter on. Am I doing something wrong? I would love to be able to put all 4 variables in the array and get rid of the excess code.

Please refer to the sections below: 'Filter data on 1002 Based SOF workbook (part 1) & (part 2)

Thanks in advance for the help!

Code:
Sub FilterCopySOF()
  
'Define variables
Dim wbBudget    As Workbook  'Budget & Execution Tool workbook
Dim wbSOF       As Workbook  '1002_Based_SOF workbook located in MDA Daily Reports
Dim LastDate    As Date      'Last modified date of 1002_Based_SOF
Dim LastRow     As Long      'Last row of SOF DAI worksheet in the GM 1-N Budget & Execution Tool workbook
  
'Delete old data from SOF DAI worksheet and turn off filter
Application.CutCopyMode = False
Set wbBudget = ActiveWorkbook
With wbBudget.Sheets("SOF DAI")
  .Range("A2", .Cells(.Rows.Count, .Columns.Count)).Clear
End With
wbBudget.Sheets("SOF DAI").AutoFilterMode = False
   
'Open and Activate the 1002 Based SOF from the server
Set wbSOF = Workbooks.Open("xxx/1002_Based_SoF.xlsx")
wbSOF.Activate

'Retrieve Date of 1002 Based SOF
LastDate = FileDateTime("xxx/1002_Based_SoF.xlsx")
wbBudget.Sheets("Report Set-Up").Range("Date_SOF").Value = LastDate

'Filter data on 1002 Based SOF workbook (part 1)
wbSOF.Sheets(1).Select
Range("A1").AutoFilter
Range("A1").AutoFilter Field:=2, Criteria1:=Array("2015", "2016"), Operator:=xlFilterValues
Range("A1").AutoFilter Field:=3, Criteria1:=Array("=2P_SO08*", "=X3_*"), Operator:=xlFilterValues
   
'Copy and Paste the 1002 Based SOF (part 1)
Application.CutCopyMode = False
wbSOF.Sheets(1).Range("Table1").SpecialCells(xlCellTypeVisible).Copy
wbBudget.Sheets("SOF DAI").Range("A2").PasteSpecial xlPasteAll
Application.CutCopyMode = False
   
'Filter data on 1002 Based SOF workbook (part 2)
wbSOF.Sheets(1).AutoFilterMode = False
wbSOF.Sheets(1).Select
Range("A1").AutoFilter
Range("A1").AutoFilter Field:=2, Criteria1:=Array("2015", "2016"), Operator:=xlFilterValues
Range("A1").AutoFilter Field:=3, Criteria1:=Array("=S3_*", "=Y3_*"), Operator:=xlFilterValues
   
'Copy, Paste, and Close the 1002 Based SOF (part 2)
wbSOF.Sheets(1).Range("Table1").SpecialCells(xlCellTypeVisible).Copy
With wbBudget.Sheets("SOF DAI")
  LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
wbBudget.Sheets("SOF DAI").Range("A" & LastRow + 1).PasteSpecial xlPasteAll
Application.CutCopyMode = False
wbSOF.Close SaveChanges:=False
   
'Activate Budget Workbook again and turn on filter
wbBudget.Activate
If Not wbBudget.Sheets("SOF DAI").AutoFilterMode Then
  wbBudget.Sheets("SOF DAI").Range("A1").AutoFilter
End If

'Go to SOF Report Page and prompt user to validate data
wbBudget.Sheets("SOF Rpt").Select
wbBudget.Sheets("SOF Rpt").Range("A1").Select
wbBudget.Sheets("SOF Rpt").Range("A1").Activate
Dim Output As Integer
Output = msgbox("Please verify error checking." & vbCrLf & _
                "" & vbCrLf & _
                "All deltas in cells D2:I3 should equal zero." & vbCrLf & _
                "" & vbCrLf & _
                "If there is an error, refer to the User Manual for help.", vbOKOnly, "Validate Data")
      
'Clear Memory
Set wbSOF = Nothing
Set wbBudget = Nothing
    
End Sub
 
Last edited:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi

See if this example helps. Tell me if you need more detailed explanations.

Excel Workbook
ABCDEF
1Customer IDYearProduct CodeYearProduct Code
2c120122P_SO08120152P_SO08*
3c220132P_SO0822016X3_*
4c320153PU
5c420152P_SO084
6c520162P_SO085
7c62016X3_1
8c72016X3_2
9c82019X3_3
10c92020X3_4
Customers

Code:
Sub AdvFilt()
[a1:c10].AdvancedFilter xlFilterInPlace, [e1:f3], , 0
End Sub
 
Upvote 0
Thanks for the response! I was able to replicate what you did and it worked. However, I couldn't get it to work on my dataset. I wonder if the data being in a table is causing problems.
 
Upvote 0
Can you post the table's first ten lines? Or post a link to the worksheet? Or email it to me?

A table per se should not be a problem.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,432
Messages
6,124,860
Members
449,194
Latest member
HellScout

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