Looping a MkDir function

anichols

Board Regular
Joined
Mar 11, 2021
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I am trying to create a batch of folders where the code is normally quite simple:
VBA Code:
MkDir[D3&E3]
I have several hundred that I regularly make. So far, I just have the same line repeated, but I'm trying to do some sort of loop or check to look for values in a column, and if present create the folder. So far I have tried several variations, and have been unsuccessful, so any help would be greatly appreciated. (My error hits at the "Then" portion as I try to add the Row value to the MkDir)

VBA Code:
Sub CreateTheMonthlyFolders()
Dim Lastrow As Long: Dim Frow As Long
Sheets("FOLDERStest").Select

Lastrow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
    For Frow = 5 To Lastrow
        If Range("B" & Frow).Value <> "" Then MkDir [("D"&Frow)&("E"&Frow)]  'normally MkDir[D5&E5]
    Next Frow
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Your best bet is to combine the cell values into a string and pass that to the MkDir function. i've trimmed out the select and added a reference to the worksheet so we're always looking in the right place.

VBA Code:
Option Explicit

Sub CreateTheMonthlyFolders()

    Dim Lastrow As Long
    Dim fRow As Long
    Dim ws As Worksheet
    Dim fPath As String
    
    Set ws = ThisWorkbook.Sheets("FOLDERStest")

    With ws
        Lastrow = .Range("B" & Rows.Count).End(xlUp).Row
        
        For fRow = 5 To Lastrow
            
            If .Range("B" & fRow).Value <> "" Then
                fPath = .Range("E" & fRow) & .Range("F" & fRow)
                MkDir fPath
            End If
        Next fRow
    End With
    
End Sub
 
Upvote 0
Solution
Your best bet is to combine the cell values into a string and pass that to the MkDir function. i've trimmed out the select and added a reference to the worksheet so we're always looking in the right place.

VBA Code:
Option Explicit

Sub CreateTheMonthlyFolders()

    Dim Lastrow As Long
    Dim fRow As Long
    Dim ws As Worksheet
    Dim fPath As String
   
    Set ws = ThisWorkbook.Sheets("FOLDERStest")

    With ws
        Lastrow = .Range("B" & Rows.Count).End(xlUp).Row
       
        For fRow = 5 To Lastrow
           
            If .Range("B" & fRow).Value <> "" Then
                fPath = .Range("E" & fRow) & .Range("F" & fRow)
                MkDir fPath
            End If
        Next fRow
    End With
   
End Sub
That worked perfectly, thank you!
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,434
Members
448,961
Latest member
nzskater

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