Consulting

Results 1 to 3 of 3

Thread: Help With VBA Document Counter

  1. #1
    VBAX Regular
    Joined
    Jun 2019
    Posts
    44
    Location

    Help With VBA Document Counter

    I'm trying to create a macro that would auto calculate the number of PDF's in the folder with the active workbook (needing it to be a relative reference as I'll be using this in multiple projects) and add +1 to it, input that number in Cell "H2", and (Ideally) have the macro run once every time that the spreadsheet is opened, rather than having it loop constantly. Could someone please help me adjust the code provided to allow for these features? Thanks in advance for your time and assistance!

    Sub sample()
            Dim FolderPath As String, path As String, count As Integer
            FolderPath = ThisWorkbook & "\"
            path = FolderPath & "\*.pdf"
            Filename = Dir(path)
            Do While Filename <> ""
                 count = count + 1
                 Filename = Dir()
            Loop
            Range("H2").Value = count
            'MsgBox count & " : files found in folder"
    End Sub
    Last edited by Baiano42; 07-11-2019 at 12:43 PM.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    In the ThisWorkbook module

    Option Explicit
    
    
    Private Sub Workbook_Open()
        Dim FolderPath As String, Filename As String, count As Long
        
        FolderPath = ThisWorkbook.path & "\*.pdf"
        
        Filename = Dir(FolderPath)
        
        Do While Filename <> ""
             count = count + 1
             Filename = Dir()
        Loop
        
        ActiveSheet.Range("H2").Value = count
            
    End Sub
    
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello Baiano42,

    Here is another method.

    Private Sub Workbook_Open()
        Dim Files As Object
    
            With CreateObject("Shell.Application")
                Set Files = .Namespace(ThisWorkbook.Path).Items
                    Files.Filter 64, "*.pdf"
                    Worksheets(1).Range("H2").Value = Files.Count
            End With
    End Sub
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •