简体   繁体   English

VBA Excel 循环遍历同目录下的all.xslm文件

[英]VBA Excel loop through all .xslm files in the same directory

Good afternoon,下午好,

I would like to check all files in my directory.我想检查我目录中的所有文件。 For this purpose, I decided to loop through all of them.为此,我决定遍历所有这些。

The good code I found here:我在这里找到的好代码:

https://www.thespreadsheetguru.com/the-code-vault/2014/4/23/loop-through-all-excel-files-in-a-given-folder https://www.thespreadsheetguru.com/the-code-vault/2014/4/23/loop-through-all-excel-files-in-a-given-folder

and changed it consequently for my personal purpose.并因此出于我的个人目的对其进行了更改。

 Sub LoopAllExcelFilesInFolder()

 Dim wb As Workbook
 Dim myPath As String
 Dim myFile As String
 Dim myExtension As String
 Dim FldrPicker As FileDialog

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

Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

With FldrPicker
  .Title = "Select A Target Folder"
  .AllowMultiSelect = False
    If .Show <> -1 Then GoTo NextCode
    myPath = .SelectedItems(1) & "\"
End With

 'In Case of Cancel
 NextCode:
 myPath = myPath
  If myPath = "" Then GoTo ResetSettings

 'Target File Extension (must include wildcard "*")
  myExtension = "*.xlsm*"

 'Target Path with Ending Extention
  myFile = Dir(myPath & myExtension)

  'Loop through each Excel file in folder
   Do While myFile <> ""
  'Set variable equal to opened workbook
    Set wb = Workbooks.Open(Filename:=myPath & myFile)

'Ensure Workbook has opened before moving on to next line of code
  DoEvents

'Change First Worksheet's Background Fill Blue
  wb.Worksheets(1).Range("A1:Z1").Interior.Color = RGB(51, 98, 174)

'Save and Close Workbook
  wb.Close SaveChanges:=True
  
'Ensure Workbook has closed before moving on to next line of code
  DoEvents

'Get next file name
  myFile = Dir
   Loop

'Message Box when tasks are completed
 MsgBox "Task Complete!"

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

  End Sub

but it looks like the code works for the first file only.但看起来该代码仅适用于第一个文件。

I am not only one with this problem, because I found the similar problems here:我不仅有这个问题,因为我在这里发现了类似的问题:

VBA Loop through excel workbooks in folder and copy data - Not looping through all files VBA 循环浏览文件夹中的 excel 工作簿并复制数据 - 不循环浏览所有文件

Excel-VBA Loop not looping through all files in folder Excel-VBA 循环不遍历文件夹中的所有文件

在此处输入图像描述

Is there a way to make this code working for all files instead of one?有没有办法让这段代码适用于所有文件而不是一个文件?

or should I use better For Each instead of Do While?还是我应该使用更好For Each而不是Do While?

My problem is very similar to this issue:我的问题与这个问题非常相似:

Code Stopping While Looping through files on workbook.close 循环访问 workbook.close 上的文件时代码停止

the new file is not prompted at all.根本不提示新文件。 In my VBA console is "no project selected"在我的 VBA 控制台中是“未选择项目”

在此处输入图像描述

I have seemingly the same code and it works fine.我似乎有相同的代码,它工作正常。

When i pickup some code somewhere i tend to make small changes step by step and make sure its still working every change i make.当我在某处拾取一些代码时,我倾向于逐步进行小的更改,并确保我所做的每一个更改都能正常工作。

 Sub LoopThroughFilesvieux()

        Dim xFd As FileDialog
        Dim xFdItem As Variant
        Dim xFileName As String
        Dim xWB As Workbook

        Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
        If xFd.Show = -1 Then

        xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
        xFileName = Dir(xFdItem & "*.xls*")

        On Error Resume Next
        Do While xFileName <> ""

          Set xWB = Workbooks.Open(xFdItem & xFileName)
          With xWB

'yourcode

          End With
    xWB.Close
    xFileName = Dir
    Loop
    End If
    End Sub

You can probably start again from my structure or the original structure you took that from and add your code lines little by little, also, try to run it step by step to see where it exits.您可能可以从我的结构或您从中获取的原始结构重新开始,并一点一点地添加您的代码行,也可以尝试逐步运行它以查看它的退出位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM