title: How to loop though a table and access row items by their column header?
body: <p>I have the following macro which needs to loop though an Excel-2007 table. The table has several columns and I am currently finding the correct column position using the <code>Index</code> property columns.</p>
  
  <p>Using the index is the only way I could find to correctly index into the <code>fName</code> object. The better option I am hoping for is to access specific columns using the Column Name/Header. How can I do this and can this be even done?</p>
  
  <p>Furthermore, in general, is there a better way to construct this loop?</p>
  
  <pre><code>Worksheets("Lists").Select
  
  Dim filesToImport As ListObject 
  Dim fName As Object
  Dim fileNameWithDate As String
  
  Dim newFileColIndex As Integer
  Dim newSheetColIndex As Integer
  Set filesToImport = ActiveSheet.ListObjects("tblSourceFiles")
  
  newFileColIndex = filesToImport.ListColumns("New File Name").Index // &lt;- Can this be different?
  
  For Each fName In filesToImport.ListRows // Is there a better way?
      If InStr(fName.Range(1, col), "DATE") &lt;&gt; 0 Then
          // Need to change the ffg line to access by column name
          fileNameWithDate = Replace(fName.Range(1, newFileColIndex).value, "DATE", _
                                    Format(ThisWorkbook.names("ValDate").RefersToRange, "yyyymmdd"))
          wbName = OpenCSVFIle(fPath &amp; fileNameWithDate)
          CopyData sourceFile:=CStr(fileNameWithDate), destFile:=destFile, destSheet:="temp"
      End If
  
  Next fName2
  </code></pre>