title: delete multiple columns by name in excel using vb.net
body: <p>I have a large spreadsheet with many columns. A lot of those columns will not be used and need to be deleted. When I wrote the procedure in VBA, I wrote a procedure that deleted all columns except for the ones listed. Can I do the same in VB.net? </p>
  
  <p>I researched the web and saw how to do it by using column A, Column B and so on. I cannot do it this way because this is an export file and the column order changes every time. The only constant is the column names, those do not change.</p>
  
  <p>Here is what I wrote in VBA.</p>
  
  <pre><code>    Sub prepareExports()
  
  'This is Step 3 when the frmDataImportSplash is activated.
  
  'This procedure deletes the first 7 rows of the sheets and
  'deletes all columns that won't be used during the process.
  
  Application.ScreenUpdating = False
  
  
  For Each WS In Sheets(Array("byEmployee", "byPosition"))
  
  With WS
  
  
  
      k = .UsedRange.Columns.Count
  
      'For the referenced sheets above delete all columns except for the ones listed below.  These columns the reference column for the entire workbook.
  
      For i = k To 1 Step -1
          Select Case LCase$(.UsedRange.Cells(1, i))
             Case "#ees", "annu. base at target", "annualized fte base pay", "annu. base mkt - 10th", "annu. base mkt - 25th", "annu. base mkt - 50th", "annu. base mkt - 75th", _
              "annu. base mkt - 90th", "annual base max", "annual base mid", "annual base min", "annualized base max", "annualized base mid", "annualized base min", _
              "annualized compa-ratio", "annualized range penetration", "employee dept", "employee id", "employee name", _
              "functional area", "hourly at target", "hourly base max", "hourly base mid", "hourly base min", "hourly compa-ratio", "hourly mkt - 10th", _
              "hourly mkt - 25th", "hourly mkt - 50th", "hourly mkt - 75th", "hourly mkt - 90th", "hourly range penetration", "hourly rate", "job code", _
              "market percentile of annu. base", "market percentile of hourly rate", "market percentile of salary mid", _
              "mid to annu. target delta | %", "mid to hourly target delta | %", "internal title", "salary", "salary at target", "salary compa - ratio", _
              "salary range penetration", "target market-ratio", "market percentile of annu. base", "Annu. Base Delta $ | %", "exemption", _
              "salary mkt - 10th", "salary mkt - 25th", "salary mkt - 50th", "salary mkt - 75th", "salary mkt - 90th", "salary at target", "mid to salaried target delta | %", _
              "tcc mkt - 10th", "tcc mkt - 25th", "tcc mkt - 50th", "tcc mkt - 75th", "tcc mkt - 90th", "grade"
  
  
                      'do nothing
              Case Else
                  .UsedRange.Columns(i).Delete
  
                  Application.ScreenUpdating = True
  
          End Select
      Next i
    End With
  Next WS
  
  End Sub
  </code></pre>