Advanced Filtering using VBA

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
793
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello - I have a little of modification I am looking to do for my existing VBA. My existing code will open a file, format it, export it to a new workbook then save it as (basically same information below minus column A). the modification i am looking for is to be able to put a criteria in a range and only the data I input will present itself in the export, the caveat being the criteria doesnt end up being in my export file at all. so my export ends up being really just columns C & D. So for this example I only want AA, CC, EE to be in the export

Book2
ABCDEFGHI
1TypeCategoryUnique TagPriceExport
2AA1ABC110AA
3AA2BBB120CC
4AA3CCC110.5EE
5AA4DDD122
6BB1ACD123
7BB2ADD145
8BB3AFF195
9BB4AGG178
10CC1LLL145
11CC2LLL265
12CC3LLL337
13CC4YYY419
14DD2UUU882
15DD3PPP034
16DD4MNM182
17EE1AFD171
18EE2DF2117
Sheet1
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
sorry forgot my code

commadelimited = formats and saves the file as CSV
c2 = flips between files to copy and paste data

VBA Code:
Sub C()
    Dim sPath       As String
    Dim sPartial    As String
    Dim sFName      As String
  
Application.ScreenUpdating = False
  
'Clear Sheet
With Sheets("C")
    .Cells.ClearContents
End With
  
'Clear C2 Upload Sheet
With Sheets("C2")
   .Range("A2:V" & rows.count).ClearContents
End With
  
    sPath = "MYPATH\"      ' <<<<< change accordingly
  
    sPartial = "MYPATH_" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.csv"
    sFName = Dir(sPath & sPartial)
    If Len(sFName) > 0 Then
        Workbooks.OpenText sPath & sFName

With Sheets("mypathxx_")
   .Range("A:Z").Copy
End With

Workbooks("Compare").Sheets("C").Range("A1").PasteSpecial
Workbooks("Compare").Sheets("C").Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

CommaDelimited2

Workbooks(sFName).Close SaveChanges:=False
C2

Application.ScreenUpdating = True

    Else
        MsgBox "File not found.", vbExclamation
    End If

End Sub
 
Upvote 0
Put the criteria to filter in column "I" of sheet "C" (as you put in your example of I2:I4)

The following code opens the csv file, filters column A, copies columns C to D, and pastes on sheet "C" in cell A1.

VBA Code:
Sub C()
  Dim sPath As String, sPartial As String, sFName As String
  Dim shC As Worksheet
  
  Application.ScreenUpdating = False
  
  Set shC = Sheets("C")
  shC.Range("A:B").ClearContents
  Sheets("C2").Range("A2:V" & Rows.Count).ClearContents
  
  sPath = "MYPATH\"      ' <<<<< change accordingly
  sPartial = "MYPATH_" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.csv"
  sFName = Dir(sPath & sPartial)
  If Len(sFName) > 0 Then
    Workbooks.OpenText sPath & sFName
    With ActiveSheet
      .Range("A1").AutoFilter 1, Application.Transpose(shC.Range("I2", shC.Range("I" & Rows.Count).End(3))), xlFilterValues
      .AutoFilter.Range.Range("C1:D" & .Range("A" & Rows.Count).End(3).Row).Copy shC.Range("A1")
    End With
    
    CommaDelimited2
    Workbooks(sFName).Close SaveChanges:=False
    C2
  Else
    MsgBox "File not found.", vbExclamation
  End If
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,804
Messages
6,121,652
Members
449,045
Latest member
Marcus05

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