Get Subfolder names into an array

MM91

Board Regular
Joined
Nov 29, 2021
Messages
59
Office Version
  1. 365
Platform
  1. Windows
Hello I am trying to populate an array with subfolder names based on a path. I can get the varient "s" to get the correct data but when I try to populate an array it says arguement not optional. please help!

Sub SFfinder()
Dim SerialnoSF As Collection, vFile
Dim serialnofolderarr As String
Dim i As Integer
Dim numsf As Integer

serialnofp = "C:\Users\mattr\Desktop\Projects\Serial Numbers\" & serialnobase


Dim fs, f, f1, s, sf
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(serialnofp)
Set sf = f.SubFolders
For Each f1 In sf
s = s & f1.Name
s = s & vbCrLf
Next

SerialnoSF = s

numsf = SerialnoSF.Count
ReDim serialnofolderarr(1 To numsf)

For i = 1 To numsf
serialnofolderarr(i) = SerialnoSF(i)
Next


'MsgBox serialnosub


MsgBox Join(serialnofolderarr, vbCrLf)
End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
OK I think i figured it out it seems to be working, if there is a more effeciant way for me to do this please let me know!

Sub SFfinder()
Dim serialnofolderarr() As String
Dim i As Integer
Dim numsf As Integer

serialnofp = "C:\Users\mattr\Desktop\Projects\Serial Numbers\" & serialnobase


Dim fs, f, f1, s, sf
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(serialnofp)
Set sf = f.subfolders
numsf = f.subfolders.Count
i = 1
ReDim serialnofolderarr(1 To numsf)
For Each f1 In sf
s = s & f1.Name
s = s & vbCrLf
serialnofolderarr(i) = s
i = i + 1
s = ""
Next


MsgBox Join(serialnofolderarr, vbCrLf)
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,264
Members
449,075
Latest member
staticfluids

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