Subfolders collection of FilesystemObject has a count

CharlesChuckieCharles

Well-known Member
Joined
May 10, 2011
Messages
2,155
Subfolders collection of FilesystemObject has a count but you can't reference it?

Subfolders.Count = 12 prefect

Folder.Subfolders(11) not allowed - errors Error 5 Invalid procedure call or argument

likewise

Folder.subfolders.Item(X)

Is the Count of any use other than to tell how many there are?
 
Last edited:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
If I have 12 sub folders in a folder

why can't I use


For x = 0 to Subfolders.Count -1

Debug.Print Subfolders(x)

next x

Or

For x = 0 to Subfolders.Count -1

Debug.Print Subfolders.Item(x).Name

next x

etc
 
Upvote 0
Because the subfolders collection is indexed by name, not number. It's more efficient to iterate using For Each anyway.
 
Upvote 0
Thanks Rory for that pointer

I found another explaination ( that if it had been in the HELP or on MSDN would have been enough )

"Cerian Knight ::
Count is already part of the primary layer by design, just type Dir at a command prompt to see the number.
If you read the count and store it, then delete a file, and then re-read the count, it should be different than what you stored.
Your stored value could cause problems in that case.
The same would apply to an index if you stored it for reuse.
Would it point to the same file if you tried to use it? Sure, probably, but for how long?
The problem is that there is no mechanism in the directory structure to pin a file to an index location for any guaranteed amount of time. "


But does that mean the Subfolders collection is updated by processes outside (ie other user updates to a network directory being reflected in my running code)... I think not
 
Upvote 0
All it really means is that relying on a numeric index is no guarantee that you would get a specific file (much like using Worksheets(1) in Excel). It's not relevant to looping by index number to simply process every file but, as I said, For Each is generally better for that anyway.
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

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