title: Looping through file extensions, excel vba
body: <p>I am using an Array of File extensions and looping through a folder of workbooks. The code is naming Sheet(1).name="MyName"</p>
  
  <p>I notice that even though <code>"*.xlsm"</code> is not in the array, it is still opening and naming the sheet.</p>
  
  <h2><a href="https://i.stack.imgur.com/U6dxx.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U6dxx.jpg" alt="enter image description here"></a></h2>
  
  <p>Here's the code. Can anybody see if they get the same problem and are able to solve it.</p>
  
  <pre><code>Sub LoopThroughFolder()
  
      Dim MyFile As String, Str As String, MyDir As String, Wb As Workbook
      Dim Rws As Long, Rng As Range
      Dim fExt, ext
      Set Wb = ThisWorkbook
      'change the address to suite
      MyDir = "C:\TestWorkBookLoop\"
      ChDir MyDir
      Application.ScreenUpdating = 0
      Application.DisplayAlerts = 0
      fExt = Array("*.xlsx", "*.xls")    'file extensions, set the file extensions of the files to move
  
      For Each ext In fExt    'loop through file extensions
          MyFile = Dir(MyDir &amp; ext)
  
  
          Do While MyFile &lt;&gt; ""
              Workbooks.Open (MyFile)
              Sheets(1).Name = "MySheet"
  
              With ActiveWorkbook
                  .Save
                  .Close
              End With
  
              MyFile = Dir()
          Loop
      Next ext
  End Sub
  </code></pre>