2016-07-04 80 views
0

enter image description here計數* .pdf文件的電子表格

我有以下的Excel電子表格,tracker.xls 2列:類別和計數。

我想遍歷一個包含與我的電子表格中的類別列匹配的pdf文件的子目錄。

enter image description here

這是我到目前爲止已經完成,但我的代碼似乎並不奏效:

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 

我似乎無法弄清楚如何計數分配給一個小區。任何想法如何?

回答

1

你在正確的軌道上,但是它遠遠比簡單:

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」不變,以配合您的出發目錄。

+0

謝謝。如果起始目錄是xlsm文件的保存位置,該怎麼辦?它不能被自動識別嗎? – methuselah

+1

是使用'ActiveWorkbook.Path'。 – Parfait