Problems creating folders

Willow123

New Member
Joined
Dec 29, 2018
Messages
17
Hi There,

I'm trying to create a program that will create a number of folders for a person. I have the creating part figured out but I'm having troubles specifying in what folder I want these new folders created in. I tried re-working some code I have that will pull file names out of a specific folder but unfortunately I can't get it to work for my intended purposes. Appreciate any thoughts on this. Please code below:

Sub ScriptingRunLibrary()

Dim fso As Scripting.FileSystemObject
Dim NewFolderPath As String
Dim s1 As String
Dim Target As String
Dim OldFolder As Scripting.Folder

Target = InputBox("Please Paste Target Folder Path Here")

FolderPath = Target

Set fso = New Scripting.FileSystemObject
Set OldFolder = fso.GetFolder(Target)

Range("C1").Select

For Each r In Range(ActiveCell.Offset(1, 0), ActiveCell.End(xlDown))
s1 = ActiveCell.Value
If Not fso.FolderExists(s1) Then
fso.CreateFolder s1
End If
ActiveCell.Offset(1, 0).Select
If ActiveCell.Value = "" Then
Exit For
End If
Next r

Set fso = Nothing

End Sub

Cheers

Trapper
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
See whether this works for you. Note the two separate functions, both dependencies of the main procedure.

VBA Code:
Sub ScriptingRunLibrary_v2()

    Dim fso As Scripting.FileSystemObject
    Set fso = New Scripting.FileSystemObject

    Dim RootFolder As String
    RootFolder = BrowseForFolderName

    Dim r As Excel.Range, NewFolder As String
    For Each r In Range("C2:C" & Cells(Rows.Count, "C").End(xlUp).Row)
        NewFolder = ProperFolderPath(RootFolder) & r.Value
        If Not fso.FolderExists(NewFolder) Then
            fso.CreateFolder NewFolder
        End If
    Next r
    Set fso = Nothing
End Sub

Public Function BrowseForFolderName(Optional ByVal argFolder As String = "") As String
    With Excel.Application.FileDialog(msoFileDialogFolderPicker)
        If VBA.CreateObject("Scripting.FileSystemObject").FolderExists(argFolder) Then
            .InitialFileName = ProperFolderPath(argFolder)
        Else
            .InitialFileName = VBA.Environ("userprofile") & "\Documents\"
        End If
        If .Show Then
            BrowseForFolderName = .SelectedItems(1)
        End If
    End With
End Function

Public Function ProperFolderPath(ByVal argPath As String) As String
    Do While VBA.Right(argPath, 1) = Excel.Application.PathSeparator
        argPath = VBA.Left(argPath, VBA.Len(argPath) - 1)
    Loop
    ProperFolderPath = argPath & Excel.Application.PathSeparator
End Function
 
Upvote 0
Solution

Forum statistics

Threads
1,215,368
Messages
6,124,520
Members
449,169
Latest member
mm424

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