【发布时间】:2017-07-07 15:35:21
【问题描述】:
我需要一些关于我的 excel vba 的帮助。
首先让我告诉它应该做什么......
在网络文件夹中,有一些 pdf 文件应该被计算在内。 文件夹如下所示:
X:/Tests/Manufact/Prod_1/Machine/Num/Year/Month/TEST_DDMMYYYY_TIMESTAMP.PDF
X:/Tests/Manufact/Prod_2/Machine/Num/Year/Month/TEST_DDMMYYYY_TIMESTAMP.PDF
X:/Tests/Manufact/Prod_3/Machine/Num/Year/Month/TEST_DDMMYYYY_TIMESTAMP.PDF
每年和每个月都有一个文件夹,其中的 pdf 根据创建日期进行排序。 计数的文件应作为带有文件名和日期的列表列在活动工作表中。 之后,我想计算在给定时间之间的特定日期创建了多少 pdf 文件。应该在像
这样的新工作表中Date - Time-Period 1 (0AM-6AM) - Time Period 2 (6AM-10AM) - Time Period 3 (10AM - 12AM)
01.01.2017 - 12PDFs - 17PDFs - 11PDFs
02.01.2017 - 19PDFs - 21PDFs - 5PDFs
也许还有一种记忆的方式,所以脚本不再计算之前已经列出的所有文件? (因为有超过 100k 的 pdf,而且每天都在增加......)
所以...我在互联网上搜索了整整一周的解决方案,我找到了一些,最终得到了以下代码:
Sub ListFiles()
Const sRoot As String = "X:\Tests\Manufact\"
Dim t As Date
Application.ScreenUpdating = False
With Columns("A:E")
.ClearContents
.Rows(1).Value = Split("File,Date,Day,Time,Size", ",")
End With
t = Timer
NoCursing sRoot
Columns.AutoFit
Application.ScreenUpdating = True
MsgBox Format(Timer - t, "0.0s")
End Sub
Sub NoCursing(ByVal sPath As String)
Const iAttr As Long = vbNormal + vbReadOnly + _
vbHidden + vbSystem + _
vbDirectory
Dim col As Collection
Dim iRow As Long
Dim jAttr As Long
Dim sFile As String
Dim sName As String
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
Set col = New Collection
col.Add sPath
iRow = 1
Do While col.count
sPath = col(1)
sFile = Dir(sPath, iAttr)
Do While Len(sFile)
sName = sPath & sFile
On Error Resume Next
jAttr = GetAttr(sName)
If Err.Number Then
Debug.Print sName
Err.Clear
Else
If jAttr And vbDirectory Then
If Right(sName, 1) <> "." Then col.Add sName & "\"
Else
iRow = iRow + 1
If (iRow And &HFFF) = 0 Then Debug.Print iRow
Rows(iRow).Range("A1:E1").Value = Array(sName, _
FileDateTime(sName), _
FileDateTime(sName), _
FileDateTime(sName), _
FileLen(sName))
End If
End If
sFile = Dir()
Loop
col.Remove 1
Loop
End Sub
它的作用是计算目录中的所有文件(因此缺少告诉它只计算 PDF 的东西)。
它确实列出了我工作表中的文件,我对那部分很满意,但它只列出了它。我仍然需要排序部分,所以要么只让它计算日期和时间段,要么让它先计算/列出所有内容,然后排序并只计算列表中的日期和时间段(我真的不知道哪个会更好,也许有简单的方法和困难的方法?)
所以如果有人知道如何做到这一点,请告诉我,我非常感谢您的帮助!
最好的问候 - 一月
【问题讨论】:
-
我认为这就像在您的
Dir命令中指定“*.PDF”一样简单,但是您使用的代码将文件和目录视为相同的东西,这样就可以仅查看带有.PDF后缀的子文件夹。我认为this answer 中的代码对另一个问题更适合您。 (您可能需要将strFolder = TrailingSlash(strFolder)的行更改为If Right(strFolder, 1) <> "/" Then strFolder = strFolder & "/"。)
标签: vba excel pdf excel-2010