在文件夹中计算* .pdf文件并更新电子表格中的相邻列

在这里输入图像说明

我有以下Excel电子表格,tracker.xls与2列:类别和计数。

我想通过一个包含与我的电子表格中的类别列匹配的pdf文件的子目录进行循环。

在这里输入图像说明

这是我到目前为止所做的,但我的代码似乎并没有工作:

Sub CV() Function CVCount() CategoryArray = Range("A2:A3") CountArray = Range("B2:B3") For i = 1 To UBound(CategoryArray) For j = 1 To UBound(CategoryArray, 2) 'get name of category Dim Category As String Category = (myarray(i, j)) FolderPath = Category + "\" path = FolderPath & "\*.pdf" Filename = Dir(path) For k = 1 To UBound(CountArray) For l = 1 To UBound(CountArray, 2) 'get count Do While Filename <> "" count = count + 1 Filename = Dir() Loop 'assign count to cell Range("").Value = count Next k Next j Next i End Function End Sub 

我似乎无法弄清楚如何为细胞分配计数。 任何想法如何?

你走在正确的轨道上,但它比这更简单:

  Option Explicit Private Const baseFolder As String = "C:\Users\Hazel\Documents\Personal\Accounts\Childcare\" Sub countFiles() Dim path As String Dim fileName As Variant Dim count As Integer Dim i As Integer i = 1 Do While Range("A" & i + 1).Value <> "" count = 0 i = i + 1 path = baseFolder & Range("A" & i).Value & "\" fileName = Dir(path) Do While fileName <> "" If UCase$(Right(fileName, 3)) = "PDF" Then count = count + 1 fileName = Dir() Loop Range("B" & i).Value = count Loop End Sub 

只需更改“baseFolder”常量以匹配您的起始目录。