Create folders based on cell values in Excel + create hyperlink

16,334
Sub MakeFolders()
    Dim Rng As Range
    Dim maxRows, maxCols, r, c As Integer
    Set Rng = Selection
    maxRows = Rng.Rows.Count
    maxCols = Rng.Columns.Count
    For c = 1 To maxCols
    r = 1
    Do While r <= maxRows
        If Len(Dir(ActiveWorkbook.Path & "\" & Rng(r, c), vbDirectory)) = 0 Then
            MkDir (ActiveWorkbook.Path & "\" & Rng(r, c))
            On Error Resume Next
        End If
        r = r + 1
    Loop
   Next c
End Sub

This VBA is to create folders, but you have to select the cells that contain the folder names before running this code Also you cannot write / and \ in Windows you can use \ for folders but not names of folders

Reserved Characters and Names

Most common characters can be used in naming files. However, the following characters are reserved and cannot be used in a file name:

< > : " / \ | ? *

Also, neither a space nor a period can be used at the end of a name. Further, files cannot have the following reserved device names: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Note that the case does not matter in Windows.

It is explained on this link.

Share:
16,334

Related videos on Youtube

Luc
Author by

Luc

Updated on September 18, 2022

Comments

  • Luc
    Luc almost 2 years

    I have the following cell values (Column FOLDERNAME) for which I'd like to automatically create a folder in my current spreadsheet active directory.

    How to do that, this is my first question?

    Inside each folder I'd like the next value (Column DRAWING_FILE) to be displayed as a hyperlink.

    How to do that, this is my second question?

    Screenshot

    • Prasanna
      Prasanna over 8 years
      Which Operating system are you on? Windows? Mac? What Excel version are you using?
    • Aganju
      Aganju over 8 years
      What did you try already?
    • Luc
      Luc over 8 years
      I'm on Win 7 Pro, using MS Excel 2010
  • Burgi
    Burgi over 8 years
    I've attempted to fix your formatting. Could you possibly explain why the OP can't use \ or _ characters?
  • Luc
    Luc over 8 years
    The VBA works for creating folders but it seems that \ can't be use either. Any luck at creating the hyperlink within each folders?
  • Roberto Barile
    Roberto Barile over 8 years
    @Burgi it is explained in this link vlaurie.com/computers2/Articles/filenames.htm
  • Roberto Barile
    Roberto Barile over 8 years
    @Luc \ correspond to a parent folder you should create it first or modify the VBA code to create it before the other folders