仅通过选定的文件循环

时间:2017-03-11 10:51:25

标签: excel vba loops

您是否知道如何仅通过VBA文件夹中的选定文件进行循环? 我设法做了循环遍历所有文件的代码。但是,我想缩短处理时间,使其只打开我在代码中指定的文件。

Sub CopyDataFromCSTR()

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim i As Long



  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual


myPath = ActiveWorkbook.Path & "\"


  myExtension = "*.xlsx*"

  myFile = Dir(myPath & myExtension)

'attempt to set a criteria for the files to open - however the loop ends at first attempt
  Do While InStr(myFile, "Trans") <> 0

      Set wb = Workbooks.Open(Filename:=myPath & myFile)

    'do something to the opened file



      wb.Close SaveChanges:=False


      DoEvents


      myFile = Dir
  Loop

  Set wb = Nothing

  MsgBox ("Done!")

ResetSettings:
  'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True




End Sub

3 个答案:

答案 0 :(得分:1)

你之前有*的地方。在替换myExtension =&#34; .xlsx &#34;中,将*替换为文件的文件掩码。 例如: &#34; FilesLikeThis.xlsx *&#34; 您可能希望将变量重命名为FileMask或更合适的名称。

例如,对于以单词hello开头的文件:

    myFileName = "hello*"
    myExtension = "xlsx*"
    myFile = Dir(myPath & myFileName & "." & myExtension)

不要忘记为新变量添加Dim。

答案 1 :(得分:1)

这是从我最喜欢的树行走程序修改而来的。我省略了递归调用,因此只搜索指定的文件夹。通常我设置引用,但在这里我已经绑定了脚本对象。

对于这个POC,我们只需打开文件,debug.print名称并关闭它

Sub ScanFldr(sFld As String, sPat As String)
    Dim fso As Object, fld As Object, fil As Object
    Dim wkb As Excel.Workbook

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fld = fso.GetFolder(sFld)
    If Right(sFld, 1) <> "\" Then sFld = sFld & "\"

    For Each fil In fld.Files
        If fil.Name Like sPat And Not (fil.Name Like "~$*" or fil.Name = thisWorkBook.Name) Then
            Set wkb = Workbooks.Open(Filename:=sFld & fil.Name, ReadOnly:=True, UpdateLinks:=False)
            Debug.Print "Opened " & fil.Name
            wkb.Close savechanges:=False
        End If
        DoEvents
    Next
    Set wkb = Nothing
    Set fso = Nothing
End Sub

打电话,例如。

ScanFldr thisworkbook.path, "*.xlsx*"

请注意,要使用thisworkbook.path,您需要在运行宏之前保存包含代码的工作簿。

答案 2 :(得分:0)

  

例如,我想只打开文件名中其他字符具有特定字符串的文件; “反式”。文件名可能如下所示:20170311sdfsdfTransfasdfasd.xlsx或20170310fasdfTransasdfaw.xlsx。有没有办法将INSTR函数合并到识别我们想要选择的文件中?

Sub CopyDataFromCSTR()

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim i As Long

  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual

myPath = ActiveWorkbook.Path & "\"

  myExtension = "*.xls*"
  myFile = Dir(myPath & myExtension)


  Do While myFile <> 0
 if instr(myFile, "trans") > 0 then    'Check condition inside the loop instead of ending the do-while loop abruptly
      Set wb = Workbooks.Open(Filename:=myPath & myFile)
    'do something to the opened file
      wb.Close SaveChanges:=False
      DoEvents
   End if
      myFile = Dir
  Loop

  Set wb = Nothing    
  MsgBox ("Done!")

ResetSettings:
  'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub