Tag: dir

Dir循环在指定的文件夹中缺less一个文件

我写了一个macros来处理指定文件夹中所有文件中的数据。 但是,它跳过文件夹中的第一个文件。 问题是在这一行中引用了第一个文件: FileName = Dir(path) 但下一个文件是用这一行引用的: FileName = Dir() 完整代码: Sub data_gatherer() 'skips ESAM_50 'Removes unrealistic data and sums the no. starts/hours run for each pump stream Application.ScreenUpdating = False Dim sheet As Worksheet Dim calcSheet As Worksheet Dim path As String Dim ColCount As Integer Dim StreamCode As String Dim StreamSum As Double […]

find最近的文件,返回一分钟内最后的x个文件

我所处的情况如下:我需要返回文件夹中最新文件的path。 我需要返回的文件数量是由“ numberOfFiles ”指定的,并且是从最近的降序。 例如, File1.doc – Last modified at 8:42:00 PM File2.doc – Last modified at 8:43:00 PM File3.doc – Last modified at 8:44:00 PM numberOfFiles = 2,应该返回一个数组; File3.doc's path File2.doc's path 这很多工作,与下面的代码。 Option Explicit Sub test() Dim FileName As String Dim FileSpec As String Dim MostRecentFile As String Dim MostRecentDate As Date Dim […]

VBA运行时错误'424'对象所需的问题

我试图将两张表传递给Excel VBA中的一个不同的子例程,以对这些表进行一些操作。 最终,我试图结合多个工作表中的数据,并删除每个列表中find的所有重复数据。 我将其定义为一个对象: Set wb1 = Workbooks.Open(Pathname & Filename) Set newWB = Workbooks.Add 然后我只是想要一个函数: Call ThisSubroutine(wb1.Sheets("Sheetnumber1"), newWB.Sheets("Sheet2")) 我得到一个运行时错误'424'对象必需对话框。 我确信这里有一个明显的解决scheme,但我忽略了一些东西。 这个小写是: Sub ThisSubroutine(Sourcefile As Worksheet, Targetfile As Worksheet) 根据要求,我添加了整个代码: Sub MergeDuplicates(ByVal DuplicateFilename As String) 'used ByVal because I was getting a "ByRef argument type mismatch" error; don't know why this happens with Dir function, as […]

excel 2010 vba,path/文件访问错误

在下面的excel 2010 vba如果提示的回答是否则te文件夹中的文件,那么该文件夹将被删除。 但是,我运行vba时遇到了path/file access error 。 具体来说, RmDir MyFolder行是突出显示的,但是当我逐步浏览代码时,正确的目录出现在variablesMyFolder ,我可以将新文件写入目录。 我错过了什么? 谢谢 :)。 另外,如果我手动导航到目录,我可以删除它。 iYesNo = MsgBox("Do the patients and barcode match the setup sheet?", vbYesNoCancel) Select Case iYesNo Case vbYes GoTo Line2 Case vbNo MsgBox ("Doesn't match! Please enter again") MyFolder = Directory ' delete all txt files in the folder MyFile = […]

列出包含关键字的所有子文件夹

我发现一个Excel VBAmacros列出了一个文件夹的所有子文件夹,但是我需要的是只列出在其名称中包含特定关键字的子文件夹。 我不知道从哪里开始。 这是我迄今为止: Sub ShowFolderList2() Dim fs, f, f1, fc, s, Keyword As String Dim folderspec Keyword = "test" folderspec = CurDir() Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.GetFolder(folderspec) Set fc = f.SubFolders For Each f1 In fc s = s & f1.name s = s & vbCrLf Next Debug.Print folderspec Debug.Print s […]

将工作簿保存为* .xml到子文件夹时出现Dir问题

我有一个小脚本允许我遍历当前文件夹中的所有xslx文件,并将它们全部保存为xml工作表。 这工作正常,但我想保存在一个子文件夹,这就是事情出错的地方,因为我总是再次保存相同的文件 。 我不太熟悉Dir语法,所以如果有人能帮我一下,我会很感激。 这部分按预期工作: Sub XLS2XML() Application.DisplayAlerts = False Dim folderPath As String Dim Report As String Dim ReportName As String Dim XMLLocation As String Dim XMLReport As String Dim WB As Workbook 'set path to current location folderPath = ThisWorkbook.Path If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\" 'loop through […]

Dir()和FSO都丢失1个文件

所有的search尝试find解决这个问题已经拿出了我所期待的相反。 我不需要从一个文件夹中的search中排除文件,但包括所有文件。 我的问题是,我的search正在返回除了1以外的文件夹中的所有文件。每次没有find1文件是完全随机的。 我曾尝试使用Dir()和FSO方法,不同的目录,不同数量的文件等无论我尝试,总是从列表中丢失1个文件。 这里是我的代码简化片段: Dir()版本: FilePath = "C:\Test\" SourceFile = Dir(FilePath & "*.xls*") Do While SourceFile <> "" SourceFile = Dir() ActiveCell.Value = SourceFile ActiveCell.Offset(1, 0).Activate Loop FSO版本: Set FileSystem = CreateObject("Scripting.FileSystemObject") DoFolder FileSystem.GetFolder(FilePath) Sub DoFolder(Folder) Dim SubFolder For Each SubFolder In Folder.SubFolders DoFolder SubFolder Next Dim File For Each File In Folder.Files If […]

在VBA中search文件

我写了一个vba代码,浏览所有的path文件夹,并search“strings.xml”文件。 Dim oFS As Office.FileSearch Dim i As Integer Set oFS = Application.FileSearch With oFS .NewSearch .FileType = msoFileTypeAllFiles .Filename = "strings.xml" .LookIn = "D:\Workspace" .SearchSubFolders = True .Execute MsgBox "Finish ! " & .FoundFiles.Count & " item found !" End With 然而,在我的工作区中,我有许多“strings.xml”文件,这个当前的代码所在,但我只想在特定的子文件夹中find“strings.xml” 例如./values/strings.xml文件。