比较在VBA中修改的文件日期
因此,我正在用VBA编写一个代码,打开文档中的所有文件,并复制和粘贴每个文档中的信息。代码被设置为打开每个文档,但它本身除外。我的困境是,我想让代码打开在主文件修改的最后一天之后修改过的文档。基本上,我想比较两个日期,其中一个日期保持不变,另一个在每次循环(每次循环新文档)后都会更改。我的代码如下,任何帮助或建议都将不胜感激。谢谢比较在VBA中修改的文件日期,vba,excel,datemodified,Vba,Excel,Datemodified,因此,我正在用VBA编写一个代码,打开文档中的所有文件,并复制和粘贴每个文档中的信息。代码被设置为打开每个文档,但它本身除外。我的困境是,我想让代码打开在主文件修改的最后一天之后修改过的文档。基本上,我想比较两个日期,其中一个日期保持不变,另一个在每次循环(每次循环新文档)后都会更改。我的代码如下,任何帮助或建议都将不胜感激。谢谢 Sub LoopThroughDirectory() Dim MyFile As String Dim erow Dim Filepath As String Dim
Sub LoopThroughDirectory()
Dim MyFile As String
Dim erow
Dim Filepath As String
Dim DateMaster As Date
Dim DateReflections As Date
Filepath = "Path of folder where all the documents are"
MyFile = Dir(Filepath)
DateReflections = FileDateTime(Filepath)
DateMaster = FileDateTime("Filepath of master document I'm comparing to")
Do While Len(MyFile) > 0
If MyFile = "zmasterfile.xlsm" Then
Exit Sub
If DateReflections < DateMaster Then
Exit Sub
End If
Workbooks.Open (Filepath & MyFile)
Range("B4:N4").Copy
ActiveWorkbook.Close
erow = Sheet1.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Reflections").Range(Cells(erow, 2), Cells(erow, 14))
MyFile = Dir
Loop
End Sub
Sub-LoopThroughDirectory()
将MyFile设置为字符串
暗色
将文件路径设置为字符串
作为日期的日期管理器
将日期映射为日期
Filepath=“存放所有文档的文件夹路径”
MyFile=Dir(文件路径)
DateReflections=FileDateTime(文件路径)
DateMaster=FileDateTime(“我比较的主控文档的文件路径”)
当Len(MyFile)>0时执行
如果MyFile=“zmasterfile.xlsm”,则
出口接头
如果DateMaster<则
出口接头
如果结束
工作簿.打开(文件路径和MyFile)
范围(“B4:N4”)。副本
活动工作簿。关闭
erow=Sheet1.单元格(Rows.Count,2).结束(xlUp).偏移量(1,0).行
ActiveSheet.Paste目标:=工作表(“反射”)。范围(单元格(erow,2),单元格(erow,14))
MyFile=Dir
环
端接头
您不应该在if语句中退出sub。您可以考虑将语句更改为以下内容:
Sub LoopThroughDirectory()
Dim MyFile As String
Dim erow
Dim Filepath As String
Dim DateMaster As Date
Dim DateReflections As Date
Filepath = "Path of folder where all the documents are"
MyFile = Dir(Filepath)
DateReflections = FileDateTime(Filepath)
DateMaster = FileDateTime("Filepath of master document I'm comparing to")
Do While Len(MyFile) > 0
DateReflections = FileDateTime(Filepath)
If MyFile <> "zmasterfile.xlsm" and DateReflections > DateMaster Then
Workbooks.Open (Filepath & MyFile)
Range("B4:N4").Copy
ActiveWorkbook.Close
erow = Sheet1.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Reflections").Range(Cells(erow, 2), Cells(erow, 14))
End If
MyFile = Dir
Loop
End Sub
Sub-LoopThroughDirectory()
将MyFile设置为字符串
暗色
将文件路径设置为字符串
作为日期的日期管理器
将日期映射为日期
Filepath=“存放所有文档的文件夹路径”
MyFile=Dir(文件路径)
DateReflections=FileDateTime(文件路径)
DateMaster=FileDateTime(“我比较的主控文档的文件路径”)
当Len(MyFile)>0时执行
DateReflections=FileDateTime(文件路径)
如果MyFile“zmasterfile.xlsm”和DateReflections>DateMaster,则
工作簿.打开(文件路径和MyFile)
范围(“B4:N4”)。副本
活动工作簿。关闭
erow=Sheet1.单元格(Rows.Count,2).结束(xlUp).偏移量(1,0).行
ActiveSheet.Paste目标:=工作表(“反射”)。范围(单元格(erow,2),单元格(erow,14))
如果结束
MyFile=Dir
环
端接头
您不应该在if语句中退出sub。您可以考虑将语句更改为以下内容:
Sub LoopThroughDirectory()
Dim MyFile As String
Dim erow
Dim Filepath As String
Dim DateMaster As Date
Dim DateReflections As Date
Filepath = "Path of folder where all the documents are"
MyFile = Dir(Filepath)
DateReflections = FileDateTime(Filepath)
DateMaster = FileDateTime("Filepath of master document I'm comparing to")
Do While Len(MyFile) > 0
DateReflections = FileDateTime(Filepath)
If MyFile <> "zmasterfile.xlsm" and DateReflections > DateMaster Then
Workbooks.Open (Filepath & MyFile)
Range("B4:N4").Copy
ActiveWorkbook.Close
erow = Sheet1.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Reflections").Range(Cells(erow, 2), Cells(erow, 14))
End If
MyFile = Dir
Loop
End Sub
Sub-LoopThroughDirectory()
将MyFile设置为字符串
暗色
将文件路径设置为字符串
作为日期的日期管理器
将日期映射为日期
Filepath=“存放所有文档的文件夹路径”
MyFile=Dir(文件路径)
DateReflections=FileDateTime(文件路径)
DateMaster=FileDateTime(“我比较的主控文档的文件路径”)
当Len(MyFile)>0时执行
DateReflections=FileDateTime(文件路径)
如果MyFile“zmasterfile.xlsm”和DateReflections>DateMaster,则
工作簿.打开(文件路径和MyFile)
范围(“B4:N4”)。副本
活动工作簿。关闭
erow=Sheet1.单元格(Rows.Count,2).结束(xlUp).偏移量(1,0).行
ActiveSheet.Paste目标:=工作表(“反射”)。范围(单元格(erow,2),单元格(erow,14))
如果结束
MyFile=Dir
环
端接头
您只需重置循环中的日期反射
,使用MyFile
构建文件路径。见下文
If MyFile = "zmasterfile.xlsm" Then
Exit Sub
DateReflections = FileDateTime(Filepath & "\" & MyFile)
If DateReflections < DateMaster Then
Exit Sub
End If
如果MyFile=“zmasterfile.xlsm”则
出口接头
DateReflections=FileDateTime(文件路径&“\”&MyFile)
如果DateMaster<则
出口接头
如果结束
另一方面,如果您想跳过文件并继续处理,而不是完全退出子文件,请将
Exit子文件
s替换为continue Do
,您只需重置循环中的DateReflections
,使用MyFile
构建文件路径。见下文
If MyFile = "zmasterfile.xlsm" Then
Exit Sub
DateReflections = FileDateTime(Filepath & "\" & MyFile)
If DateReflections < DateMaster Then
Exit Sub
End If
如果MyFile=“zmasterfile.xlsm”则
出口接头
DateReflections=FileDateTime(文件路径&“\”&MyFile)
如果DateMaster<则
出口接头
如果结束
另外,如果您想跳过文件并继续处理,而不是完全退出子文件,请将您的
Exit子文件
s替换为continue Do
重构的好建议重构的好建议谢谢您的帮助!Datereflections起作用,但continue不起作用,并且两个文档日期的比较不正确。有什么想法吗?谢谢谢谢你的帮助!Datereflections起作用,但continue不起作用,并且两个文档日期的比较不正确。有什么想法吗?谢谢