VBAvalidation文件扩展名为excel文件?

我运行这个VBA,通过文件夹,并把它汇编在一张大单中的数据。 我的问题是我得到的隐藏文件称为thumbs.db的错误,我需要添加一些东西,以便它validation它只是用xlsx扩展名拉文件。 以下是我正在使用的代码。

Sub DoFolder(Folder) Dim SubFolder As Folder Dim i As Integer Dim CopyR As Range For Each SubFolder In Folder.SubFolders DoFolder SubFolder Next If Folder.SubFolders.Count = 0 Then If Folder.Files.Count = 1 Then If Mid(Folder.Files, Len(Folder.Files) - 3, 4) = "xlsx" Then Else: MsgBox "2+ files: " & Folder.Path End If End If For Each File In Folder.Files Hoover File Next Else End If End Sub 

我遇到的问题是

 If Mid(Folder.Files, Len(Folder.Files) - 3, 4) = "xlsx" Then 

任何帮助,将非常感激

Folder.Files是一个集合而不是一个string。

recursion文件search:

 Sub DoFolder(FolderName As String, Optional fso As Object) Dim f As Object, MySubFolder As Object, RootFolder As Object Dim cFiles As Collection If fso Is Nothing Then Set fso = CreateObject("Scripting.FileSystemObject") Set RootFolder = fso.GetFolder(FolderName) For Each MySubFolder In RootFolder.SubFolders DoFolder MySubFolder.Path, fso Next Set cFiles = New Collection For Each f In RootFolder.Files If f.Name Like "*xls*" Then cFiles.Add f Next If cFiles.Count > 0 Then MsgBox cFiles.Count & " files found in " & RootFolder.Name For Each f In cFiles Hoover f Next End If End Sub 

一个简单的解决办法就是检查包含在文件名中的xlsx 。 喜欢这个:

 If InStr(1,"FileName","xlsx",vbTextCompare)<1 then 

因此,除非有人将thumbs.db重命名为thumbsxlsx.db ,否则您将处于安全的一面。

假设你正在使用FileSystemObject,即使我们看不到声明,并且假设你只想调用Hoover获得.xlsx文件,它看起来就像是你可以使用下面的代码

 If Right(File.Name, 4) = "xlsx" Then Hoover File End If