Trouble counting files in a Directory with VBA

mvervair

New Member
Joined
Aug 3, 2016
Messages
27
I'm trying to check how many files are in a directory, increment the count each time then put that count in my file name.

I'm having trouble with the loop. I can kinda see what the reason is, it looks like it's not counting each file but possibly saying on the same one. Any ideas from the geniuses out there?

Code:
If Dir(filePath & custnm & "*.*") <> "" Then
    Do Until Dir(filePath & custnm & "*.xlsm") = ""
    x = x + 1
    Dir
    Loop
    book.SaveAs Filename:=filePath & custnm & "\" & custnm & " install form" & "v_" & x
Else:
    MkDir filePath & custnm
    book.SaveAs Filename:=filePath & custnm & "\" & custnm & " install form"
    
End If
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I use a macro that lists all of the files in a folder. Some modifications might make it work for your purposes:

Code:
Sub RecursiveFolder(objFolder As Scripting.Folder)
    
    'Declare the variables
    Dim objFile As Scripting.File
    x=0
    
    'Loop through each file in the folder
    For Each objFile In objFolder.Files
        x=x+1
    Next objFile
    
End Sub

*shrug* Give it a try!
 
Upvote 0
er, forgot to mention, you'll need to plug that into the proper section of your sub. "Some assembly required"
 
Upvote 0
Ok, that led me down the FileSystemObject rabbit hole - I hadn't used that feature(?) in VBA before. Looks like I have some reading up to do tonight!
 
Upvote 0

Forum statistics

Threads
1,216,014
Messages
6,128,287
Members
449,436
Latest member
blaineSpartan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top