title: VBA: If filename.dat in folder contains X, open, run macro, save as filename.xlsm, close
body: <p>I am trying to create a macro that will search a folder for a .dat file that contains "OPS"(not case sensitive) in the name, if it finds a file I would like to open it and run another macro, save the file as the original filename.xlsm, and close. </p>
  
  <p>So far, I am able to search for the name but that's about the extent of my knowledge.</p>
  
  <pre><code>Sub Test2()
      Dim sh As Worksheet, lr As Long, fPath As String, fName As String, rFile() As Variant
      fPath = "C:\Users\ntunstall\Desktop\test\"
  
      ctr = 1
  
      fName = dir(fPath &amp; "*.dat")
      Do Until fName = ""
  
          If InStr(fName, "OPS") &gt; 0 Then
              ReDim Preserve rFile(1 To ctr)
              rFile(ctr) = fName
              ctr = ctr + 1
          End If
  
          fName = dir
      Loop
      For i = LBound(rFile) To UBound(rFile)
          'The variable rFile(i) represents the workbooks you want to work with.
          MsgBox rFile(i)  
      Next
  End Sub
  </code></pre>
  
  <p>Ideally, this macro would run any time a .dat file containing OPS in the filename is opened. Any help is appreciated, thanks.</p>