title: Writing a macro for Excel that dynamically removes and reorganizing remaining columns from a table of data
body: <p>I have a system that I pull data from monthly for a variety of accounts and the system is regularly used by others and that changes the order of the columns, adds and subtracts columns based on various needs, and I'm looking for a way to take that dynamic report once exported, paste into one sheet of a workbook in excel, on another, allow my team to paste in the row of columns they need for their specific uses, in the order they need, then run the macro and have it remove all unnecessary columns, and reorganize the columns into the required order onto a new workbook.</p>
  <p>I have a working system for the removal of unnecessary systems, BUT it's not dynamic, as each kept column is set in the macro. (Original macro found here <a href="https://stackoverflow.com/questions/16597841/deleting-all-columns-except-columns-with-certain-headings">Deleting all columns except columns with certain headings</a>, and adjusted accordingly to my needs to create the new file and clear the old.)</p>
  <pre><code>    Sub deleteIrrelevantColumns()
      Dim keepColumn As Boolean
      Dim currentColumn As Integer
      Dim columnHeading As String
      currentColumn = 1
      While currentColumn &lt;= ThisWorkbook.Sheets(&quot;DATA&quot;).UsedRange.Columns.Count
          columnHeading = ThisWorkbook.Sheets(&quot;DATA&quot;).UsedRange.Cells(1, currentColumn).Value
          'CHECK WHETHER TO KEEP THE COLUMN
          keepColumn = False
              If columnHeading = &quot;Account Number&quot; Then keepColumn = True
              If columnHeading = &quot;Invoiced&quot; Then keepColumn = True
              If columnHeading = &quot;Create Date&quot; Then keepColumn = True
              If columnHeading = &quot;Customer Reference&quot; Then keepColumn = True
              If columnHeading = &quot;Origin Country&quot; Then keepColumn = True
              If columnHeading = &quot;Destination Country&quot; Then keepColumn = True
              If columnHeading = &quot;DestinationLocationCode&quot; Then keepColumn = True
              If columnHeading = &quot;Weight&quot; Then keepColumn = True
  
  
  
          If keepColumn Then
          'IF YES THEN SKIP TO THE NEXT COLUMN,
              currentColumn = currentColumn + 1
          Else
          'IF NO DELETE THE COLUMN
              ThisWorkbook.Sheets(&quot;DATA&quot;).Columns(currentColumn).Delete
          End If
          'LASTLY AN ESCAPE IN CASE THE SHEET HAS NO COLUMNS LEFT
          If (ThisWorkbook.Sheets(&quot;DATA&quot;).UsedRange.Address = &quot;$A$1&quot;) And (ActiveSheet.Range(&quot;$A$1&quot;).Text = &quot;&quot;) Then Exit Sub
      
      Wend
  ThisWorkbook.Sheets(&quot;DATA&quot;).Copy
  ThisWorkbook.Sheets(&quot;DATA&quot;).Cells.Clear
  End Sub
  </code></pre>
  <p>I have also found <a href="https://stackoverflow.com/questions/23702934/reordering-multiple-columns-in-excel-vba">Reordering Multiple Columns in Excel VBA</a> which looks like I can have my team paste their list of needed columns in the appropriate order into a static place on the excel file, and have it pull the range of cells into the array, but I'm not sure how to combine these two together, to make that work together. Ideally, I'd like to be able to get a drop down on one sheet for them to just select the account they would like, and the system automatically fills in all of the columns in the order they need.</p>
  <p>I'm trying to learn as much of all of this as I can, but I'm struggling with understanding how all of the VBA meshes together.</p>