MkDir with If close

frotaru

Board Regular
Joined
Oct 12, 2006
Messages
124
Value of range A1 is C:\test\01.01

MkDir range(“A1”).value

The folder it will create is 01.01

I want to know if it’s possible to create an IF close to this MkDir function that will not return any error msg

Something like if folder 01.01 exists than create other with name 02.01 .. if 02.01 exists than create 03.01 and so on


If xx.01 exists than create (xx+1).01

But xx+1 cannot be grather than 31 xx < or = 31

Can anyone help me with this pls ????
Thank you all
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Von Pookie

MrExcel MVP
Joined
Feb 17, 2002
Messages
13,686
It may not be perfect, but maybe this would help:

Code:
Sub test()
Dim x As Integer

' save the first portion of the value in A1 to variable
x = Left(Range("A1").Value, 2)

Do
    ' if error occurs, code continues
    On Error Resume Next
    
    'attempt to create the directory
    MkDir "C:\test\" & Format(x, "00") & ".01"
    
    ' if MkDir did not return an error
    ' (was able to create folder), exit the loop
    If Err.Number = 0 Then Exit Do
    
    ' If MkDir *did* return an error (folder already exists)
    ' reset error trapping
    Err.Clear
    'increase x by 1
    x = Format(x + 1, "00")
Loop 'loop and try to create folder named for new x value

End Sub
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,300
Office Version
  1. 365
Platform
  1. Windows
Something like this perhaps.
Code:
Sub NewDir()
Dim strPath As String
Dim pos As Long
    
    strPath = Range("A1")
    
    If Dir(strPath, vbDirectory) <> "" Then
        pos = InStr(strPath, ".")
        strPath = Left(strPath, pos) & Format(Mid(strPath, pos + 1) + 1, "00")
    End If
    MkDir strPath
    
    Range("A1") = strPath
    
End Sub
 

Forum statistics

Threads
1,136,594
Messages
5,676,700
Members
419,644
Latest member
KeelsM

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
Top