好的,所以我认为自己是一名Excel VBA专家(尽管我已经有一段时间没做过多了)但是我对这个问题感到困惑 - 这显然意味着它是非常简单的我忽略了由于我的傲慢:D
我正在使用分配给全局变量的FileSystemObject(称为“myFSO” - 原始,我知道......)并使用另外两个全局变量 - xFolder和xFile - 来遍历文件夹中的所有文件,并且对文件夹中的每个文件执行操作。
这是代码:
Global myFSO As FileSystemObject
Global xFolder As Scripting.Folder
Global xFile As Scripting.File
Public Sub GetData()
Set bgd = ThisWorkbook.Sheets("BGD")
Set myFSO = New FileSystemObject
Set xFolder = myFSO.GetFolder(bgd.Range("C4").Value)
For Each xFile In xFolder.Files
<do stuff here>
Next xFile
End Sub
因此,当我单步执行代码时,我可以看到正确分配了xFolder。如果我添加一个Watch或插入
Debug.Print xFolder.Files.Count
进入代码,它返回正确的文件计数,所以一切似乎都设置好进入For循环并做它需要做的事情。
一旦我跳过For Each ...语句行,但代码只运行到例程的末尾,完全错过了For Each ... Next代码中嵌套的整个代码。它甚至没有进入“Next xFile”行。如果我将循环修改为
For i = 1 to xFolder.Files.Count
以这种方式完成,它运作正常。所以,这不是生死攸关的事情,因为我可以做我想做的事情,我只是想知道是否有人能说出为什么“For Each”方法没有用。
答案 0 :(得分:2)
这是一个有用的最小例子:
BGD
的工作表,并将现有路径写入C4
,例如C:\Windows
这将列出调试窗口中的路径和所有文件名。
Public Sub GetData()
Dim bgd As Worksheet
Dim myFSO As FileSystemObject
Dim xFolder As Scripting.Folder
Dim xFile As Scripting.File
Set bgd = ThisWorkbook.Sheets("BGD")
Set myFSO = New FileSystemObject
Set xFolder = myFSO.GetFolder(bgd.Range("C4").Value)
Debug.Print xFolder.Path
For Each xFile In xFolder.Files
Debug.Print xFile.Name
Next xFile
End Sub
如果您需要本地变量,则在本地而不是全局声明它们。其他任何事情都是非常糟糕的做法,并导致错误。
答案 1 :(得分:0)
好的 - 事实证明问题实际上是文件夹而不是代码。正如@Peh所指出的,我应该用另一个文件夹测试代码,以及测试代码本身的变体。使用其他文件夹时,For Each
代码运行正常。正如在OP中所述,我只使用了For i = 1 to xFolder.Files.Count
而不是For Each
并得到了我需要的结果,但我更喜欢For Each
的对象引导方法,而不是使用整数/长变量通过项目计数,并想知道为什么该方法不起作用。
为了使For Each
代码正常工作,我将所需的文件复制到另一个文件夹,它运行得非常好。由于原始文件夹是网络位置,因此可能存在一些文件夹权限或安全设置,使我无法使用我想要的代码。
答案 2 :(得分:-1)
我认为您要列出所有文件夹和所有子文件夹中的所有文件。看看这个链接。
http://www.learnexcelmacro.com/wp/2011/11/how-to-get-list-of-all-files-in-a-folder-and-sub-folders/
下载文件;这是要走的路。在Excel工作表中列出所有路径和所有文件名后,您可以进行各种比较,操作等。
Sub GetFilesInFolder(SourceFolderName As String)
'--- For Example:Folder Name= "D:\Folder Name\"
Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.folder, SubFolder As Scripting.folder
Dim FileItem As Scripting.File
Set FSO = New Scripting.FileSystemObject
Set SourceFolder = FSO.GetFolder(SourceFolderName)
'--- This is for displaying, whereever you want can be configured
r = 14
For Each FileItem In SourceFolder.Files
Cells(r, 2).Formula = r - 13
Cells(r, 3).Formula = FileItem.Name
Cells(r, 4).Formula = FileItem.Path
Cells(r, 5).Formula = FileItem.Size
Cells(r, 6).Formula = FileItem.Type
Cells(r, 7).Formula = FileItem.DateLastModified
Cells(r, 8).Formula = "=HYPERLINK(""" & FileItem.Path & """,""" & "Click Here to Open" & """)"
r = r + 1 ' next row number
Next FileItem
Set FileItem = Nothing
Set SourceFolder = Nothing
Set FSO = Nothing
End Sub
Sub GetFilesInFolder(SourceFolderName As String, Subfolders As Boolean)
'--- For Example:Folder Name= "D:\Folder Name\" and Flag as Yes or No
Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.folder, SubFolder As Scripting.folder
Dim FileItem As Scripting.File
'Dim r As Long
Set FSO = New Scripting.FileSystemObject
Set SourceFolder = FSO.GetFolder(SourceFolderName)
'--- This is for displaying, whereever you want can be configured
r = 14
For Each FileItem In SourceFolder.Files
Cells(r, 2).Formula = r - 13
Cells(r, 3).Formula = FileItem.Name
Cells(r, 4).Formula = FileItem.Path
Cells(r, 5).Formula = FileItem.Size
Cells(r, 6).Formula = FileItem.Type
Cells(r, 7).Formula = FileItem.DateLastModified
Cells(r, 8).Formula = "=HYPERLINK(""" & FileItem.Path & """,""" & "Click Here to Open" & """)"
r = r + 1 ' next row number
Next FileItem
'--- This is the Function to go each and Every Folder and get the Files. This is a Nested-Function Calling.
If Subfolders = True Then
For Each SubFolder In SourceFolder.Subfolders
ListFilesInFolder SubFolder.Path, True
Next SubFolder
End If
Set FileItem = Nothing
Set SourceFolder = Nothing
Set FSO = Nothing
End Sub