根据文件名打开excel文件(一个接一个),具体范围为日期时间
我正在寻找一个vba代码,以帮助过滤一个特定的日期范围。开始日期和结束日期位于文件夹中格式为YYYYMMDD的文件名上,如下所示 我正在尝试打开特定日期范围内的所有文件,逐个复制其中的一些数据,然后粘贴到另一个工作簿(A_2021.xlsm) 我尝试并成功地使用了一个日期时间,但我不知道如何根据文件名过滤特定的日期范围。请看一看我的代码在案件单一日期如下,并帮助我,如果有任何想法的案件范围的日期。非常感谢!根据文件名打开excel文件(一个接一个),具体范围为日期时间,excel,vba,date-range,Excel,Vba,Date Range,我正在寻找一个vba代码,以帮助过滤一个特定的日期范围。开始日期和结束日期位于文件夹中格式为YYYYMMDD的文件名上,如下所示 我正在尝试打开特定日期范围内的所有文件,逐个复制其中的一些数据,然后粘贴到另一个工作簿(A_2021.xlsm) 我尝试并成功地使用了一个日期时间,但我不知道如何根据文件名过滤特定的日期范围。请看一看我的代码在案件单一日期如下,并帮助我,如果有任何想法的案件范围的日期。非常感谢! 要筛选特定的日期范围,您可能需要设置2个单元格来存储开始日期和结束日期,让我们将A1
要筛选特定的日期范围,您可能需要设置2个单元格来存储开始日期和结束日期,让我们将A1和A2设置为开始日期和结束日期,例如: Sub-singledateOMS() 保存所有.csv文件的文件夹路径 开始日期=CDate(此工作簿。工作表(1)。单元格(“A1”)。文本) 结束日期=CDate(此工作簿。工作表(1)。单元格(“A2”)。文本) Set date\u dic=CreateObject(“script.dictionary”) 对于i=开始日期到结束日期 日期(格式(i,“yyyyMMdd”)和“.csv”)=” 接下来我 当前文件=目录(文件夹路径&“\*.csv”) 当前_文件“”时执行此操作 如果存在日期(当前文件),则 设置eapp=CreateObject(“excel.application”) 设置efile=eapp.Workbooks.Open(文件夹路径和当前文件) 'efile是您想要的文件 '复制粘贴过程代码 埃菲尔,关上 Set efile=Nothing 退出 设置eapp=Nothing 如果结束 MyFile=Dir 如果MyFile=”“,则 退出Do 如果结束 环
End Sub这是获取文件名的方式
Option Explicit
Public Function GetFileNames(ByVal startDate As Date, ByVal endDate As Date) As Collection
' Change "C:\User\" to the name of the folder containing the data files.
' You might also decide to pass it in as a parameter.
Const path As String = "C:\User\"
Dim startFileName As String
Dim endFileName As String
Dim fileName As String
Set GetFileNames = New Collection
startFileName = Format(startDate, "YYYYMMDD") & ".CSV"
endFileName = Format(endDate, "YYYYMMDD") & ".CSV"
fileName = Dir(path & "*.csv")
Do While fileName <> ""
Select Case fileName
Case Is < startFileName
' skip
Case Is <= endFileName
GetFileNames.Add path & fileName
Case Else
Exit Do
End If
' Next file name.
fileName = Dir()
Loop
End Function
为什么要创建新的Excel实例?只需使复制/粘贴过程不可见,就可以直接使用工作簿。Open()。。。
folder_path = "" 'where you save all .csv files
start_date = CDate(ThisWorkbook.Sheets(1).Cells("A1").Text)
end_date = CDate(ThisWorkbook.Sheets(1).Cells("A2").Text)
Set date_dic = CreateObject("script.dictionary")
For i = start_date to end_date
date_dic(Format(i, "yyyyMMdd") & ".csv") = ""
Next i
current_file = Dir(folder_path & "\*.csv")
Do While current_file <> ""
If date_dic.exists(current_file) Then
Set eapp = CreateObject("excel.application")
Set efile = eapp.Workbooks.Open(folder_path & "\" & current_file)
'efile is the file you want
'copy paste process codes
efile.Close
Set efile = Nothing
eapp.Quit
Set eapp = Nothing
End If
MyFile = Dir
If MyFile = "" Then
Exit Do
End If
Loop
Option Explicit
Public Function GetFileNames(ByVal startDate As Date, ByVal endDate As Date) As Collection
' Change "C:\User\" to the name of the folder containing the data files.
' You might also decide to pass it in as a parameter.
Const path As String = "C:\User\"
Dim startFileName As String
Dim endFileName As String
Dim fileName As String
Set GetFileNames = New Collection
startFileName = Format(startDate, "YYYYMMDD") & ".CSV"
endFileName = Format(endDate, "YYYYMMDD") & ".CSV"
fileName = Dir(path & "*.csv")
Do While fileName <> ""
Select Case fileName
Case Is < startFileName
' skip
Case Is <= endFileName
GetFileNames.Add path & fileName
Case Else
Exit Do
End If
' Next file name.
fileName = Dir()
Loop
End Function
Public Sub Main()
Dim fileName As String
' You need to provide the start & end dates
For Each fileName In GetFileNames(startDate, endDate)
' Open file, read, copy data, close, etc.
Next fileName
End Sub