Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a folder and sub folder in Excel VBA

I have a pull down menu of companies that is populated by a list on another sheet. Three columns, Company, Job #, and Part Number.

When a job is created I need a folder for said company and a sub-folder for said Part Number.

If you go down the path it would look like:

C:\Images\Company Name\Part Number\

If either company name or Part number exists don't create, or overwrite the old one. Just go to next step. So if both folders exist nothing happens, if one or both don't exist create as required.

Another question is there a way to make it so it works on Macs and PCs the same?

like image 354
Matt Ridge Avatar asked May 29 '12 17:05

Matt Ridge


People also ask

How do I create a folder and subfolder in Excel?

1. Select the cell values that you want to create folders and subfolders based on. 2. Then click Kutools Plus > Import & Export > Create Folders from Cell Contents to open the Create Folders from Cell Contents dialog box.

How do I create a folder using VBA?

The Microsoft Excel MKDIR statement allows you to create a new folder or directory. The MKDIR function is a built-in function in Excel that is categorized as a File/Directory Function. It can be used as a VBA function (VBA) in Excel.


1 Answers

Another simple version working on PC:

Sub CreateDir(strPath As String)     Dim elm As Variant     Dim strCheckPath As String      strCheckPath = ""     For Each elm In Split(strPath, "\")         strCheckPath = strCheckPath & elm & "\"         If Len(Dir(strCheckPath, vbDirectory)) = 0 Then MkDir strCheckPath     Next End Sub 
like image 88
Martin Avatar answered Sep 23 '22 15:09

Martin