Add subfolders and name - Possible?

T

Trish Smith

Hi,

I was wondering if it would be possible to add subfolders to a folder using
vba?

If it is possible I would like to be able to add subfolders from a list of
names in an Excel sheet.

If anyone could help that would be great :)

Thank you
 
O

OssieMac

Hi Trish,

The following code example should help. Take particular note of the comments.

To make a folder based on a value in a cell then simply make the variable
equal to the cell and ensure that you get the path info correct.

Sub PathsAndThings()

Dim strCurDir As String
Dim strApplicationPath As String
Dim strThisWorkBookPath As String
Dim strMyNewFolder As String

'Current directory is the path where a workbook will be
'saved by default if you click save.
'When Excel is first opened it is the default path
'as set in options but if you use Save As and select another
'path to save the workbook, then that path becomes the
'Current Directory. The same if you select another path
'to open a workbook.
strCurDir = CurDir

MsgBox "The current directory (or path) is " & strCurDir


'Application path is the path of the Excel application
strApplicationPath = Application.Path

MsgBox "The path for the current application is " & strApplicationPath

'This workbook path is the path of the current open workbook
strThisWorkBookPath = Application.ThisWorkbook.Path

MsgBox "The application current project path is " & strThisWorkBookPath

'To create a directory (or folder) use MkDir function
'if you use the function on its own as follows then
'it creates a sub-folder of the Current directory

MkDir "Test folder"

'To make a folder at another location then precede the folder
'with the required path.

MkDir "C:\Users\Username\Test folder"

'Variables can be used in lieu of the actual name
'Note that the back slash added to the
'string before the file name because the code
'Application.ThisWorkbook.Path does not return the
'backslash at the end

strMyNewFolder = strThisWorkBookPath & "\Test folder"

MkDir mynewfolder

End Sub
 
J

Joel

Sub addsubfolders()

strfolder = "c:\temp"
Set fso = CreateObject _
("Scripting.FileSystemObject")
Set Myfolder = _
fso.GetFolder(strfolder)
RowCount = 1
Do While Range("A" & RowCount) <> ""
Newfolder = Range("A" & RowCount)
fso.createfolder (strfolder & "\" & Newfolder)
RowCount = RowCount + 1
Loop

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top