excel - 如何指定顺序 Dir() 查看 vba 中的文件
问题描述
我正在使用 excel vba 中的 Dir() 函数对文件夹中的文件进行排序并对每个文件执行一些转换。Dir() 默认情况下按文件名的字母顺序浏览文件。我希望能够指定 Dir() 函数通过文件的顺序。具体来说,我希望它按修改日期的顺序遍历文件。
宏遍历用户指定的文件夹,在 excel 中打开文件夹中的 .txt 文件,格式化数据,计算数据的统计数据(平均值、标准差等),然后将这些统计数据复制到另一个 excel 文件中。我发现这个问题按字母顺序对文件进行排序,但我需要按修改日期对它们进行排序。我已经复制了下面代码的相关部分。我不知道如何让语法突出显示工作,所以如果难以理解,我提前道歉。
Sub MahloDataToText()
Dim folder_to_search As String
Dim full_path As String
Dim the_file_name As String
folder_to_search = ActiveSheet.Cells(4, "D").Value
Const pattern_recognition As String = "*.txt"
the_file_name = Dir(folder_to_search & pattern_recognition, vbNormal)
Do While Len(the_file_name) > 0
full_path = folder_to_search & the_file_name
Workbooks.OpenText Filename:=full_path, Origin:=437, StartRow:=5, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, _
Comma:=False, Space:=False, Other:=False
'here is where all the formatting, statistics, and copying take place. Excluded for space'
the_file_name = Dir
Loop
End Sub
基本上问题在于 Dir() 函数打开文件的顺序。它按字母顺序打开它们,我需要按修改日期的顺序打开它们。我已经尝试按照我想要的方式手动对文件夹进行提前排序,但这似乎不会影响 Dir() 函数。
如果有办法让我指定打开文件的顺序,那么这将解决问题。
解决方案
推荐阅读
- javascript - 使用起点、终点和航点创建 MapKit JS
- amazon-web-services - 如何从 Google Cloud SQL 迁移到 AWS RDS
- node.js - 为什么即使“URI”是假的或错误的,node.js 总是说我已连接到我的 MongoDB?
- python - 具有 Eager Execution 的 Tensorflow:DDPG - 将动作梯度应用于 Actor
- php - getClientOriginalName() on null 和 $request->hasFile('image') 不起作用
- swift - 使用组合的运行时错误:dyld:找不到符号:_$s7Combine9PublishedV9PublisherCyx_GAadAMc
- java - url [http://localhost:9000/batch/index] 返回的状态无效:[504]
- hangout - 在卡片文本中实现等宽文本
- rstudio - 如何在 Rmd 文件中应用 80 个字符的行宽?
- sql - 将 varchar 更改为 bigint