[英]Is there method similar to 'Find' available when we Loop through folder (of files) using Dir Function in excel vba?
As we know, we use Find() method to find whether a string or any Microsoft Excel data type exists in an excel. 众所周知,我们使用Find()方法来查找Excel中是否存在字符串或任何Microsoft Excel数据类型 。
(Usually we do it on set of data) (通常我们会根据数据集执行此操作)
I want to know if any such method available when we loop through folder(of files) using Dir function. 我想知道在使用Dir函数遍历文件夹(文件)时是否有这种方法可用。
Situation: 情况:
I have an excel - ' FileNames.xlsx ' in which 'Sheet1' has names of files having extensions .pdf
/ .jpg
/ .jpeg
/ .xls
/ .xlsx
/ .png.
我有一个excel-“ FileNames.xlsx ”,其中“ Sheet1”具有扩展名为
.pdf
/ .jpg
/ .jpeg
/ .xls
/ .xlsx
/ .png.
的文件名.png.
/ .txt
/ .docx
/ .rtf
in column A. /
.txt
/ .docx
/ .rtf
在A列中。
I have a folder named ' Folder ' which has most(or all) of the files from 'FileNames.xlsx'. 我有一个名为“ Folder ”的文件夹 ,其中包含“ FileNames.xlsx”中的大多数(或全部)文件。
I have to check whether all the file-names mentioned in the 'FileNames.xlsx' exist in 'Folder'. 我必须检查“文件夹”中是否存在“ FileNames.xlsx”中提到的所有文件名。
For this I have written the below VBScript(.vbs): 为此,我编写了以下VBScript(.vbs):
strMessage =Inputbox("Enter No. of Files in Folder","Input Required")
set xlinput = createobject("excel.application")
set wb123 =xlinput.workbooks.Open("E:\FileNames.xlsx")
set sh1 =wb123.worksheets("Sheet1")
For i = 2 to strMessage +1
namei = sh1.cells(i,1).value
yesi = "E:\Folder"+ namei +
If namei <> yesi Then
sh1.cells(i,1).Interior.Color = vbRed
Else
End If
Next
msgbox "Success"
xlinput.quit
As I wasn't able to get the required Output I tried it recording a small Excel VBA Macro. 由于无法获得所需的输出,因此我尝试记录了一个小的Excel VBA宏。 (Changed FileNames.xlsx to FileNames.xlsm)
(将FileNames.xlsx更改为FileNames.xlsm)
Sub LoopThroughFiles()
Dim lastRow As Long
lastRow = Sheets("Sheet1").UsedRange.Rows.Count
Dim MyFolder As String
Dim filename As Range
Dim MyFile As String
MyFolder = "E:\Folder"
For Each filename In Worksheets("Sheet1").Range("A2A:" & lastRow)
MyFile = Dir(MyFolder & "\*.xlsx")
'Here I actually need to pass all file extensions to Dir
Do While MyFile <> ""
If filename = MyFile Then
'Do Nothing
Else
filename.Interior.Color = vbRed
MyFile = Dir
Next
End Sub
The above is a failed attempt. 以上是失败的尝试。
I thought of trying it with method similar to Find() 我想到用类似于Find()的方法尝试
Sub LoopThroughFiles()
Dim lastRow As Long
'Dim LastFile As Long
'Is there need of it (LastFile variable)? I kept this variable
'to save (prior known) count of files in folder.
lastRow = Sheets("Sheet1").UsedRange.Rows.Count
'LastFile = 'Pass count of Files in folder to this variable.
Dim fileName As Range
For Each fileName In Worksheets("Sheet1").Range("A2:A" & lastRow)
Dim rngFnder As Range
On Error Resume Next
'Error at below line.
Set rngFnder = Dir("E:\Folder\").Find(filename)
'This line gives me error 'Invalid Qualifier'
'I am trying to use method similar to Find()
If rngFnder Is Nothing Then
filename.Interior.Color = vbRed
End If
Next
End Sub
But, I couldn't achieve the result. 但是,我无法达到目的。 Can anyone tell me is there any such function available to 'Find' whether all filenames in an excel exist in a folder after looping through folder using Dir ?
谁能告诉我,使用Dir遍历文件夹后,是否有任何此类功能可用于“查找” Excel中的所有文件名是否存在于文件夹中?
As per my knowledge, Dir function works with only one file extension at a time. 据我所知,Dir函数一次只能使用一个文件扩展名。 Is it possible to use Dir function for multiple file extensions at a time?
是否可以一次将Dir函数用于多个文件扩展名?
Expected Output: 预期产量:
Assume I have 8 filenames in 'FileNames(.xlsx/.xlsm)'. 假设我在'FileNames(.xlsx / .xlsm)'中有8个文件名。 Out of which Arabella.pdf and Clover.png are not found in 'Folder', Then I want to color cells for these filenames in red background in excel as in below image.
在“文件夹”中找不到其中的Arabella.pdf和Clover.png ,然后我想在Excel中以红色背景为这些文件名的单元格上色,如下图所示。
Sub LoopThroughFiles()
Dim lastRow As Long
lastRow = Sheets("Sheet1").UsedRange.Rows.Count
Dim MyFolder As String
Dim filename As Range
Dim MyFile As String
MyFolder = "E:\Folder"
For Each filename In Worksheets("Sheet1").Range("A2:A" & lastRow)
MyFile = MyFolder & "\" & filename
If Not FileExists(MyFile) Then
filename.Interior.Color = vbRed
End If
Next
End Sub
Public Function FileExists(strFullpathName As String) As Boolean
If Dir(strFullpathName) <> "" Then
FileExists = True
Else
FileExists = False
End If
End Function
You can output a list of the files that are contained in the folder. 您可以输出文件夹中包含的文件的列表。 I found a really helpful tutorial on that here: http://software-solutions-online.com/2014/03/05/list-files-and-folders-in-a-directory/#Jump1
我在这里找到了一个非常有用的教程: http : //software-solutions-online.com/2014/03/05/list-files-and-folders-in-a-directory/#Jump1
If you then loop through both the original and the output lists and look for a match. 如果然后循环浏览原始列表和输出列表,并寻找匹配项。 Easiest is to first colour them all red, and un-colour the matches.
最简单的方法是先将它们全部涂成红色,然后将火柴取消上色。 Else you would need an additional if-statement that states: When you reach the last element in the original list, and no match has been found, then colour red.
否则,您将需要一个附加的if语句来声明:当您到达原始列表中的最后一个元素,并且没有找到匹配项时,将颜色设置为红色。
Edit: For continuity's sake I copied the code bits of the link I mentioned above: 编辑:为了连续性,我复制了上面提到的链接的代码位:
Getting all file names form within 1 folder: 在1个文件夹中获取所有文件名:
Sub Example1()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("D:StuffFreelancesWebsiteBlogArraysPics")
i = 1
'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
'print file name
Cells(i + 1, 1) = objFile.Name
'print file path
Cells(i + 1, 2) = objFile.Path
i = i + 1
Next objFile
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.