VBA Code - edit

superfb

Active Member
Joined
Oct 5, 2011
Messages
255
Office Version
  1. 2007
Platform
  1. Windows
Hi i have the following VBA code to create folders - however, how can i edit this code so if i have two folders with the same name for eg

02052018
02052018


the code will make it do

02052018
02052018 - 2 ( or - 3...depending the number of times it has been copied over)

Code:
Sub MakeFolders()
    Dim xdir As String
    Dim FSO
    Dim destfol As String
    Dim lstrow As Long
    Dim i As Long
    destfol = Range("B3").Value
    Set FSO = CreateObject("Scripting.FileSystemObject")
    lstrow = Cells(Rows.Count, "f").End(xlUp).Row
    Application.ScreenUpdating = False
    For i = 8 To lstrow '<-- reads list from F2
        'change the path on the next line where you want to create the folders
        xdir = Range("B3").Value & Range("f" & i).Value
        If Not FSO.FolderExists(xdir) Then
            FSO.CreateFolder (xdir)
        End If
    Next
    Application.ScreenUpdating = True
    MsgBox i - 8 & " folders created in Directory :" & destfol
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try the following:

Code:
Sub MakeFolders()
    Dim xdir As String
    Dim FSO
    Dim destfol As String
    Dim lstrow As Long
    Dim i As Long, n As Long
    
    destfol = Range("B3").Value
    Set FSO = CreateObject("Scripting.FileSystemObject")
    lstrow = Cells(Rows.Count, "f").End(xlUp).Row
    Application.ScreenUpdating = False
    For i = 8 To lstrow '<-- reads list from F2
        'change the path on the next line where you want to create the folders
        xdir = Range("B3").Value & Range("f" & i).Value
        If Not FSO.FolderExists(xdir) Then
            FSO.CreateFolder (xdir)
        Else
            n = 2
            Do While True
                xdir = Range("B3").Value & Range("f" & i).Value & " - " & n
                If Not FSO.FolderExists(xdir) Then
                    FSO.CreateFolder (xdir)
                    Exit Do
                Else
                    n = n + 1
                End If
            Loop
        End If
    Next
    Application.ScreenUpdating = True
    MsgBox i - 8 & " folders created in Directory :" & destfol
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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