Consulting

Results 1 to 8 of 8

Thread: Solved: macro to create a directory

  1. #1
    VBAX Regular cmefly's Avatar
    Joined
    Jan 2005
    Location
    Toronto, Canada
    Posts
    18
    Location

    Solved: macro to create a directory

    Hi,

    how do i write a macro to create a directory on my H: drive?

    specificall, a folder called "data extractor"

    thanks,

    marc

  2. #2
    VBAX Regular cmefly's Avatar
    Joined
    Jan 2005
    Location
    Toronto, Canada
    Posts
    18
    Location
    Better yet....
    how do you go about writing code for:

    "if the directory called "data extractor" does not exist....create it....."

  3. #3
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Marc,

    The simple code to do it is:

    [vba]MkDir "H:\data extractor"[/vba]

    I only say simple, as you may want to implement some error handling in case it already exists. Like:

    [vba]Sub CreateDirectory()
    On Error Resume Next
    MkDir "H:\data extractor"
    If Err.Number <> 0 Then MsgBox "Directory exists!"
    On Error GoTo 0

    End Sub[/vba]

    Cheers,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  4. #4
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location


    You're too fast! Use the second option. It will accomplish what you're after!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  5. #5
    VBAX Regular cmefly's Avatar
    Joined
    Jan 2005
    Location
    Toronto, Canada
    Posts
    18
    Location
    Awesome!!! just Awesome!!

    thank you!

  6. #6
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    You're welcome,

    Now you get VBAX lesson 2, though!

    See up the top of the thread, there's a Thread Tools menu? You can jump in there and mark your own threads solved. Another slick tool developed by our board coders!

    Cheers,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  7. #7
    VBAX Newbie
    Joined
    Jan 2021
    Posts
    1
    Location
    This recursively creates directories - eg:

    NW_MkDir("C:\The\Quick\Brown\Fox\Jumped\Over\The\Lazy\Dog")



    Function NW_MkDir(NewFolderPath As String) As Boolean
      On Error Resume Next
      NW_PATH = Split(NewFolderPath, "\")
      For Each NW_FOLDER In NW_PATH
          NW_FOLDERS = NW_FOLDERS & "\" & NW_FOLDER
     '    MsgBox Mid(NW_FOLDERS, 2, 999)
          MkDir (Mid(NW_FOLDERS, 2, 999))
      Next
      If Dir(NW_FOLDER, 16) = "" Then
         NW_MkDir = False
      Else: NW_MkDir = True
      End If
    End Function

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Wrong timing (15 years), wrong method (recursion), wrong code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •