检查文件夹并比较txt文件的确切列表。还有一个带有文件夹选择器的公共变量,我无法在消息框中显示



我有一个activex按钮,试图让它检查文件夹中是否有一组特定的.txt文件。我想将所有文件名与文件名列表进行比较,看看文件夹中没有列出什么。在这个文件检查模块中还有一个publicvariable调用,用于列出文件夹路径(用户使用folderpicker选择的路径),但尚未使其工作。这个公共变量应该在Msgbox的下一行,列出所选的文件夹路径。我可以将文件列表放在工作簿中的任何位置。目前,我正在处理一个检查文件模块,无论文件是否存在,它都会返回一条消息。

我只是能够让我的公共变量发挥作用。这不是我通过阅读和学习如何提问来学习的。通过移动线条/单词,我已经能够使一些事情发挥作用。尽管如此,我相信这不是最有效的方法。


'Working but only checks one file at a time. and hard coded
Sub CheckFolderForFiles()
'
' CheckFolderForFiles Macro
'
'Check if file exist
If Dir$("C:txtdatacf_preferences.txt") = "" Then
MsgBox "C:txtdatacf_preferences.txt - File not found"
Exit Sub
End If
____________________________________
' Not working - Just testing public variable call for Dir$ and figure out MsgBox areas.
Sub CheckFolderForFiles()
'
' CheckFolderForFiles Macro
'

'检查文件是否存在如果Dir$(Module33.fle+"\alers.txt")="则MsgBox&fle&"alerts.txt-找不到文件"退出Sub结束如果''End Sub__________________________________

'Folder Picker FileDialog user select folder.  
'After some guidence by one our your users I was able to get this module work.
'Now have a public variable I wish to use throughtout the workbook to 
'call the path.

Public fle As String
Sub FolderPicker()
Dim diaFolder As FileDialog
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.Show
fle = diaFolder.SelectedItems(1)
Range("M11") = fle
Set diaFolder = Nothing
End Sub
-------------------------------
Sub CheckFolderForFiles()
'
' CheckFolderForFiles Macro
'
'Check if file exist
'
'
If Dir$(Module33.fle + "alerts.txt") = "" Then 
MsgBox Module33.fle + "alerts.txt - File not found"
End If
'
If Dir$(Module33.fle + "cf_messages.txt") = "" Then
MsgBox Module33.fle + "cf_messages.txt - File not found"
End If
End Sub
----------------------------

我正在努力学习这一点,以帮助我妻子的工作项目。所以请耐心等待我的描述和缺乏术语。但是,如果有人能引导我使用一个脚本,该脚本可以比较来自公共变量的文件夹中的文件,并告诉我列表中缺少的所有文件。(总共15个文件)这将帮助很多人。此外,有人知道如何或是否可以清除公共变量中存储的数据吗?谷歌是说只要在模块里放一个(结束)。不起作用。

提前谢谢。我非常感谢你的指导。

试试这个。我使用ArrayList过滤掉不存在的文件。如果你想打印出一个不存在文件的列表,只需打印出arraylistFileList的剩余元素,你可以在谷歌上搜索语法。

Sub TestFileExist()
Dim fd As FileDialog
Dim mFiles As Variant, Item As Variant
Dim FileList As Object, mRange As Range, strFile As String
Dim FilesInFolder() As String
Dim i As Long
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.Title = "Select a folder"
.AllowMultiSelect = False
End With
If fd.Show = -1 Then
Set FileList = CreateObject("System.Collections.ArrayList")
Set mRange = Range("A1:A5") 'Range contains files' names
ReDim FilesInFolder(0) As String
strFile = Dir(fd.SelectedItems(1) & "*.txt")
Do While Len(strFile) > 0
FilesInFolder(UBound(FilesInFolder)) = strFile
strFile = Dir
ReDim Preserve FilesInFolder(UBound(FilesInFolder) + 1) As String
Loop
For Each Item In mRange
If Not FileList.contains(Item.Value) Then
FileList.Add Item.Value
End If
Next Item
For i = 0 To UBound(FilesInFolder) - 1
If FileList.contains(FilesInFolder(i)) Then
FileList.Remove FilesInFolder(i)
End If
Next i
MsgBox FileList.Count 'Nbr of files not found
End If
End Sub

最新更新