1

I have an Excel spreadsheet which I use as a job tracker. I would like to write a code in VBA which creates a directory (with sub folders) dependent on some of the values that have been entered for each record.

Column A: PIN B: Team C: Title

In an already established directory in Windows I have a folder which contains folders of all of the teams we do work for. Basically, I want to have the code so that it looks at the team value in my tracker and creates a new folder (with sub folders) within that team area.

eg: "C:\Teams\Team 1\new folder goes here"

I would like the new folder created to use the format of the PIN + Title from columns A and C. Plus, inside each new created folder, I would like these sub-folders: "1_Comms", "2_Input", "3_Working", "4_Output".

eg: "C:\Teams\Team 1\PIN value + Title" with all of the aforementioned sub folders contained in this new directory.

Any assistance on this would be massively appreciated as I am a total noob at VBA and very much still learning.

Haven't tried anything so far other than totally embarrassing myself.

2
  • To create a folder in VBA using a cell value you can do this: Public Sub Test() MkDir "G:\To.Delete\Title" + Cells(1, 1).Value End Sub Commented Aug 19, 2019 at 11:58
  • If you received a valid answer, please mark it accordingly Commented Aug 26, 2019 at 7:58

2 Answers 2

1

To do that you first create the main folder, like in your example "C:\Teams\Team 1\PIN value + Title". So, after that you create the subfolders.

Sub CreateFolder(ByVal pin_value As String, ByVal title As String)

Dim wb_path As String
Dim folder_1_path, folder_2_path, folder_3_path, folder_4_path As String

    wb_path = ThisWorkbook.Path

    ' create main folder
    wb_path = wb_path & "\" & pin_value & "_" & title

    MkDir (wb_path)

    ' create subfolder  
    folder_1_path = wb_path & "\1_Comms"
    folder_2_path = wb_path & "\2_Input"
    folder_3_path = wb_path & "\3_Working"
    folder_4_path = wb_path & "\4_Output"

    MkDir (folder_1_path)
    MkDir (folder_2_path)
    MkDir (folder_3_path)
    MkDir (folder_4_path)
End Sub

If your PIN number is an integer, you will need convert it to string. Like in this test below:

Sub test()

    pin_value = 777
    title = "myFolder"

    pin_value = CStr(pin_value)

    Call CreateFolder(pin_value, title)

End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you both for your responses. Claudius, in your code do I simply replace all of the "ws_path" with my folder directory?
You just need put the workbook (with the code) in the directory do you want. The code: ws_path = ThisWorkbook.Path will return the folder way that you are inside. Or, if you want rigid codding, you can put the folder way directly.
1

You can use this recursive function, wich takes care of the missing folders:

Function SmartCreateFolder(byVal sFolder as String) As Boolean
'inspired by: https://stackoverflow.com/a/54280512/78522
'Works with drive letters but also with UNC paths
    Static oFSO As Object

    If oFSO Is Nothing Then Set oFSO = CreateObject("Scripting.FileSystemObject")
    On Error GoTo hell  'mostly to handle forbidden characters
    With oFSO
        If Not .FolderExists(sFolder) Then
            If SmartCreateFolder(.GetParentFolderName(sFolder)) Then
                .CreateFolder sFolder
            Else
                GoTo hell
            End If
        End If
    End With
    SmartCreateFolder = True
hell:
End Function

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.