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

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,873
Members
449,056
Latest member
ruhulaminappu

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