title: Automated sorting of files into folders using excel VBA
body: <p>I am currently trying to put a macro together to sort files into folders based on a filename. I am locked into using VBA due to the system we are on.</p>
  
  <p>For example sorting just the excel documents from below present in C:\ :</p>
  
  <pre><code>123DE.xls
  124DE.xls
  125DE.xls
  124.doc
  123.csv
  </code></pre>
  
  <p>into the following folder paths:</p>
  
  <pre><code>C:\Data\123\Data Extract
  C:\Data\124\Data Extract
  C:\Data\125\Data Extract
  </code></pre>
  
  <p>The folders are already created, and as in the example are named after the first x characters of the file. Batches of 5000+ files will need to be sorted into over 5000 folders so im trying to avoid coding for each filename</p>
  
  <p>I am pretty new to VBA, so any guidance would be much appreciated. So far I have managed to move all the excel files into a single folder, but am unsure how to progress.</p>
  
  <pre><code>Sub MoveFile()
  
  Dim strFolderA As String
  Dim strFolderB As String
  Dim strFile as String
  
  strFolderA = "\\vs2-alpfc\omgusers7\58129\G Test\"
  strFolderb = "\\vs2-alpfc\omgusers7\58129\G Test\1a\"
  strFile = Dir(strFolderA &amp; "*.xlsx*")
  
  Do While Len(strFile) &gt;0
      Name StrFolderA &amp; strFile As strFolderB &amp; strFile
      strFile = Dir
  Loop
  
  End Sub
  </code></pre>
  
  <p>Greg</p>
  
  <p>EDIT</p>
  
  <pre><code>Sub MoveFile()
  
  Dim strFolderA As String
  Dim strFile As String
  Dim AccNo As String
  
  strFolderA = "\\vs2-alpfc7\omgUSERS7\58129\G Test\"
  
  strFile = Dir(strFolderA &amp; "*.xlsx*")
  
  Do While Len(strFile) &gt; 0
  
  AccNo = Left(strFile, 2)
  
      Name strFolderA &amp; strFile As strFolderA &amp; "\" &amp; AccNo &amp; "\Data Extract\" &amp; strFile
  
      strFile = Dir
  Loop
  
  End Sub
  </code></pre>
  
  <p>Thanks folks, are a few more bits and pieces i want to add, but functionality is there!</p>