title: VBA Macro - Trying to write code that will dynamic filter excel sheet and then copy values in a certain column
body: <p>VBA Macro - Trying to write code that will dynamic filter excel sheet and then copy values with that filter applied in a certain column</p>
  <p>The scenario:
  There are two excel sheets: the original document and a newly created excel document that the data will be exported to.</p>
  <p>I have already tried recording a macro to filter the original document and then copying/pasting that code into the macro I am creating (called &quot;KPI_DATA&quot;). The copy/pasted code will not work in KPI_DATA but it works fine in the separate, recorded macro (called &quot;Macro1&quot;).</p>
  <p>What is happening?</p>
  <p>Additionally, in KPI_DATA I am attempting to set the range to all cells with entries in them so that the macro KPI_DATA can be applied to other excel documents as well. For this, I am trying to use the code &quot;ActiveSheet.UsedRange.AutoFilter Field:=3, Criterial:=&quot;2&quot;&quot; <a href="https://i.stack.imgur.com/sZF4k.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>Can anyone advise?</p>
  <p><a href="https://i.stack.imgur.com/7mMtk.png" rel="nofollow noreferrer">enter image description here</a></p>
  <pre><code>Sub Macro1()
  '
  ' Macro1 Macro
  '
  
  '
      ActiveSheet.Range(&quot;$A$4:$DB$2168&quot;).AutoFilter Field:=3, Criteria1:=&quot;2&quot;
  End Sub
  
  Sub KPI_Data()
  
      
      Dim wkb As Workbook
      Dim BOM As Workbook
  '
      'Adding New Workbook
     Set wkb = Workbooks.Add
    ' variable for the bom workbook
      Set BOM = ThisWorkbook
    
   ' Dynamic filter BOM sheet
   ActiveSheet.UsedRange.AutoFilter Field:=3, Criterial:=&quot;2&quot;
  
  ....etc
  End Sub
  </code></pre>