title: How to select a specific column by header name on VBA, and use the selected column on other actions (removeduplicates, autofilter)
body: <p>I'm trying to automate the actions required to count the leads that come from an exported csv from Unbounce. I managed to create a macro to do that to all forms that have the same set of columns. However, some clients have different forms and that generates different sets of columns in the spreadsheets, so that when I apply the macro to these clients' spreadsheets the wrong columns are selected in the functions.</p>
  
  <p>I need to be able to select the columns by the header name, so that i can apply the functions on the right columns even if they are not in the same position. </p>
  
  <p>In my mind (not with the VBA syntax) it would be something like</p>
  
  <p>if
  column header = "email"
  then 
  execute function
  else
  search next column header</p>
  
  <p>I have found these possible solutions 
  <a href="https://stackoverflow.com/questions/37686841/find-column-header-by-name-and-select-all-data-below-column-header-excel-vba">Find Column Header By Name And Select All Data Below Column Header (Excel-VBA)</a></p>
  
  <p><a href="https://www.extendoffice.com/documents/excel/4879-excel-select-column-by-header-name.html" rel="nofollow noreferrer">https://www.extendoffice.com/documents/excel/4879-excel-select-column-by-header-name.html</a></p>
  
  <p>but I could not separate the part of the code that applies only to selecting the columns and then making the following functions understand that it should work with the result of the previous selection.</p>
  
  <p>The functions I need to give the right selections of columns to are below.
  The goal is to remove duplicates form columns with header email, remove tests from columns with header name and email, and then count the leads with utm_source = facebook and utm_source = instagram.</p>
  
  <pre><code> Range("G6").Select
      ActiveSheet.Range("$A$1:$AA$2130").RemoveDuplicates Columns:=13, Header:= _
          xlYes
      Cells.Select
      Selection.AutoFilter
      Range("M5").Select
      ActiveSheet.Range("$A$1:$AA$2130").AutoFilter Field:=13, Criteria1:= _
          "=*test*", Operator:=xlAnd
      Rows("2:2159").Select
      Selection.Delete Shift:=xlUp
      Range("M2127").Select
      ActiveSheet.Range("$A$1:$AA$2125").AutoFilter Field:=13
      ActiveSheet.Range("$A$1:$AA$2125").AutoFilter Field:=12, Criteria1:= _
          "=*test*", Operator:=xlAnd
      Rows("2:2155").Select
      Selection.Delete Shift:=xlUp
      ActiveSheet.Range("$A$1:$AA$2123").AutoFilter Field:=12
      Range("N8").Select
      Sheets.Add After:=ActiveSheet
      ActiveCell.FormulaR1C1 = "facebook"
      Range("B1").Select
      ActiveCell.FormulaR1C1 = _
          "=COUNTIF('unbounce??'!C[20],""facebook"")+COUNTIF('unbounce??'!C[20],""instagram"")"
  </code></pre>