create folder based on cell content

Arie Bos

Board Regular
Joined
Mar 25, 2016
Messages
224
Office Version
  1. 365
Platform
  1. Windows
I want to create a folder in below 'Sales' folder, named after the text value located in a cell named 'CompanyName'. So if the company name in that cell is XYZ, I want to have the new folder created in 'Sales' called XYZ. I am missing something here, but can't see where or what...

VBA Code:
Public Function vDir() As String
    vDir = Environ("userprofile") & "\Documents\Soniq\Sales\" & Range("CompanyName") & "\"
End Function

Sub ProjectFolder()
    Dim vDir As String, NewFldr3 As String
        NewFldr3 = Range("CompanyName")

    If Dir(vDir & NewFldr3, vbDirectory) = "" Then
        MkDir vDir & NewFldr3
    End If
End Sub
 
Last edited:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
This should get you headed in the right direction.

Public Function vDir() As String
vDir = Environ("userprofile") & "\Documents\Soniq\Sales\" & Range("CompanyName") '''''''''''' not needed & "\"
End Function

Sub ProjectFolder()
''''''''''''''''''''''' not needed screws up the vDir function Dim vDir As String, NewFldr3 As String
'''''''''''' Not needed vDir has the new folder name NewFldr3 = Range("CompanyName")

'''wrong If Dir(vDir & NewFldr3, vbDirectory) = "" Then

If Dir(vDir, vbDirectory) = "" Then ''''Folder in the vDir function
MkDir vDir '''''''' not needed as it is in vDir & NewFldr3
End If
End Sub
 
Upvote 0
RayFrye, Thank you for stripping this down to the core essence. It shows that as a beginner one sometimes get confused by too many elements...

This is how I understand your corrections, and although it seems so ligical now, a new folder is not yet being made in 'Sales' with the name in 'CompanyName'.
VBA Code:
Public Function vDir() As String
    vDir = Environ("userprofile") & "\Documents\Soniq\Sales\" & Range("CompanyName")
End Function
Sub ProjectFolder()
     If Dir(vDir, vbDirectory) = "" Then
        MkDir vDir
    End If
End Sub
 
Upvote 0
After some more searching and trying, this one worked OK:

VBA Code:
Public Function vDir() As String
    vDir = Environ("userprofile") & "\Documents\Soniq\Sales\" & Range("CompanyName")
End Function
Sub CommandButton4_Click()
    
Dim NewDir As String
Dim ChkDir As String

NewDir = Range("CompanyName")
ChkDir = Dir(vDir, vbDirectory)
     
If ChkDir <> "" Then
    MsgBox ChkDir & " folder exists"
Else
    MkDir NewDir
    MsgBox "Created: " & NewDir
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,638
Members
449,093
Latest member
Ahmad123098

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