Consulting

Results 1 to 7 of 7

Thread: Saving File Gives Error Excel VBA

  1. #1
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location

    Saving File Gives Error Excel VBA

       If Len(Dir("Report " & Format(Date, "yyyymmdd"))) = 0 Then
       MkDir "Report " & Format(Date, "yyyymmdd")
       Else
       MsgBox "You already ran today's reports and need to delete the folder before running them again."
       Exit Sub
    I just get a Path/File access error on the MkDir line of code instead of getting the message box.
    I have tried moving the lines around, too. Anybody know what I'm doing wrong?
    ~Anne Troy

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I guess you could try checking for the folder rather than the file.
    If Len(Dir("Report " & Format(Date, "yyyymmdd"),vbDirectory)) = 0 Then     
    MkDir "Report " & Format(Date, "yyyymmdd") 
    Else 
        MsgBox "You already ran today's reports and need to delete the folder before running them again." 
        Exit Sub
    Last edited by Kenneth Hobs; 05-25-2015 at 12:21 PM.

  3. #3
    VBAX Mentor
    Joined
    Feb 2015
    Posts
    395
    Location
    MKDir (which allows you to create a new folder), looks for something ressembling:
    c:\Test\Excel

    But your vba is generates
    Report 20150525

    So I think it is asking for a drive letter and a path

    Something like
    MkDir "C:\MyDocuments\Report " & Format(Date, "yyyymmdd")
    or
    Dim MyPath as string
    MyPath = "C:\MyDocuments\"
    MkDir MyPath  & "Report " & Format(Date, "yyyymmdd")
    or even
    Dim MyPath as string
    MyPath = "C:\MyDocuments\" & "Report " & Format(Date, "yyyymmdd")
    MkDir MyPath

  4. #4
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Kenneth: I just get errors an error on ELSE when I run yours.

    Yongle: The drive and path should be the current drive and path.
    ~Anne Troy

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    What with:

    Sub M_snb()
       MsgBox CurDir & " Report " & Format(Date, "yyyymmdd")
       
       MsgBox Dir("Report " & Format(Date, "yyyymmdd"), 16)
       
       If Dir("Report " & Format(Date, "yyyymmdd"), 16) = "" Then MkDir "Report " & Format(Date, "yyyymmdd")
       
       
       MsgBox Dir("Report " & Format(Date, "yyyymmdd"), 16)
    End Sub

  6. #6
    VBAX Mentor
    Joined
    Feb 2015
    Posts
    395
    Location
    See what this throws up:
    MsgBox Application.ActiveWorkbook.Path & "Report " & Format(Date, "yyyymmdd")

  7. #7
    VBAX Mentor
    Joined
    Feb 2015
    Posts
    395
    Location
    Have just tested and:

    This line results in your error
    MkDir "Report " & Format(Date, "yyyymmdd")
    This line does not
    MkDir "\Report " & Format(Date, "yyyymmdd")

Posting Permissions

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