title: Export Filtered Worksheet as CSV File
body: <p>I am currently using this code to export the whole sheet, but I would like to only export the rows where a condition is met. In this case in Column 13 there is an "x":</p>
  
  <pre><code>Option Explicit
  Sub ExportAsCSV()
  
  Dim MyFileName As String
  Dim CurrentWB As Workbook, TempWB As Workbook
  
  Set CurrentWB = ActiveWorkbook
  ActiveWorkbook.ActiveSheet.UsedRange.Copy
  
  Set TempWB = Application.Workbooks.Add(1)
  With TempWB.Sheets(1).Range("A1")
    .PasteSpecial xlPasteValues
    .PasteSpecial xlPasteFormats
  End With        
  
  Dim Change below to "- 4"  to become compatible with .xls files
  MyFileName = CurrentWB.Path &amp; "\" &amp; Left(CurrentWB.Name, Len(CurrentWB.Name) - 5) &amp; ".csv"
  
  Application.DisplayAlerts = False
  TempWB.SaveAs Filename:=MyFileName, FileFormat:=xlCSV, CreateBackup:=False, Local:=True
  TempWB.Close SaveChanges:=False
  Application.DisplayAlerts = True
  End Sub
  </code></pre>
  
  <p>I tried to adapt the code with an autofilter but was not successful.
  I replaced the line </p>
  
  <pre><code>ActiveWorkbook.ActiveSheet.UsedRange.Copy
  </code></pre>
  
  <p>with this:</p>
  
  <pre><code>ActiveSheet.Range("$A$1:$M$81").AutoFilter Field:=13, Criteria1:="x"
  Range(Selection, Selection.End(xlToRight)).Select
  Range(Selection, Selection.End(xlDown)).Select
  Selection.Copy
  </code></pre>
  
  <p>But I am getting the Autofiltermethod of Range Class failed error.</p>