Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12

Thread: MACRO - Check/Create Folder Structure And Save

  1. #1

    Post MACRO - Check/Create Folder Structure And Save



    Register for a FREE account, and/
    or Log in to avoid these ads!

    Hi there

    I am hoping that someone can please help me, I will try and put this as best I can:

    I have an Excel spreadsheet - 3 tabs - TabA, TabB (Hidden) and TabC (Hidden).

    The user enters details into a predetermined fields onto TabA, this information is then copied (via formulas) into the hidden sheet TabB, when the user has completed entering in all the information they then click on a button to Save.

    The information on TabB is copied into TabC, this tab is then copied into a new workbook and saved as a 1 tab workbook.

    The location of where this tab needs to be saved, is compiled in cell CA1, as the destination/file name will be dependant on the information entered. I haved tried a previous Macro to check if the folder structure exists and if it does, then save the file, but if it doesn't then create the folder structure and then also save the file, however, I have hit a bit of a wall.

    I tried the coding supplied to me on my home pc and it worked fine - saving to C:\Documents and Settings\Administrator\My Documents etc etc..... however, when I tried it in my workplace, I find I am having difficultly, it is not saving in the folder structure that already exist, it is creating a whole new structure.

    The only difference I can see is the path/folder structure, although still wanting to save in "My Documents".

    When I right click on the folder it is showing the path that I need, which I entered, as it doesn't have an assigned drive I.e. C:\..... it shows as E.g. \\Dfs60325.gpn.gov.uk\1234567$\My Documents.....

    The first part being the location, "1234567$" being the users identifier etc.....

    Can anyone please please help me with my dilemma??

    Many thanks in advance.

    Mark

  2. #2
    Administrator Ken Puls's Avatar
    Join Date
    Mar 2011
    Location
    Nanaimo, BC, Canada
    Posts
    2,233
    Articles
    57
    Blog Entries
    14
    Excel Version
    Excel Office 365 Insider
    Hi Mark,

    Try this peice of code to return the user's home directory:

    Code:
    Environ("USERPROFILE") + "\My Documents"
    Let me know if that sorts it for you.
    Ken Puls, FCPA, FCMA, MS MVP (Excel)

    Learn to Master Your Data at the Power Query Academy (the world's most comprehensive online Power Query training) or with my book M is for Data Monkey!

    Main Site: http://www.excelguru.ca -||- Blog: http://www.excelguru.ca/blog -||- Forums: http://www.excelguru.ca/forums
    Check out the Excelguru Facebook Fan Page -||- Follow Me on Twitter

    If you've been given VBA code (a macro) for your solution, but don't know where to put it, CLICK HERE.

  3. #3
    You can also use

    Code:
    CreateObject("WScript.Shell").SpecialFolders("MyDocuments")
    There are a whole series of Special Folders.

  4. #4
    Hi there both, thank you for you suggestions, where would I put either of those codes please?

    This is the coding that I have in the spreadsheet: (Sorry was unsure of how to put in the box like you have)

    Sub SaveWorkflow()

    Sheets("TabB").Visible = True
    Sheets("TabC").Visible = True
    Sheets("TabB").Select
    Cells.Select
    Selection.Copy
    Sheets("TabC").Select
    Cells.Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Range("A1").Select
    Application.CutCopyMode = False


    Sheets("TabC").Copy


    Dim sDest As String
    Dim sFolder As String

    sDest = ActiveWorkbook.Worksheets("TabC").Cells(1, 79)

    sFolder = Left(sDest, InStrRev(sDest, "\"))

    CreateFolder (sFolder)
    ActiveWorkbook.SaveAs sDest

    ActiveWorkbook.Close

    MsgBox ("You Can Now Reset")

    End Sub


    '// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    '// Purpose : Will Recursively Build ADirectory Tree
    '// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Sub CreateFolder(Folder)
    On Error Resume Next
    Dim objFSO As Object: Set objFSO = CreateObject("Scripting.FileSystemObject")
    If Folder <> "" Then
    If Not objFSO.FileExists(objFSO.GetParentFolderName(Folder)) Then
    Call CreateFolder(objFSO.GetParentFolderName(Folder))
    End If
    objFSO.CreateFolder (Folder)
    End If

    End Sub


    Any help really is greatly appreciated.

    Thanks
    Mark

  5. #5
    Where you setup sDest.

  6. #6
    Hi there

    sDest is the path (E.g. \\Dfs60325.gpn.gov.uk\1234567$\My Documents..) that is currently compiled in Cell CA1

    Thanks
    Mark

  7. #7
    I thought you wanted to overwrite that with the actual My Documents folder.

  8. #8
    Hi there

    The path E.g \\Dfs60325.gpn.gov.uk\1234567$\My Documents\SP Sheets\2012-10\AEG1245 Mark.xls

    Changes with every use of the spreadsheet:

    \\Dfs60325.gpn.gov.uk = this is a constant
    1234567$ = changes with every user
    My Documents = this is a constant
    SP Sheets = this is a constant
    2012-10 = this changes depending on information, will either be a date folder, Pending or Residual Action
    AEG1245 Mark = changes depending on information entered I.e. filename

    Hope this helps.

  9. #9
    Administrator Ken Puls's Avatar
    Join Date
    Mar 2011
    Location
    Nanaimo, BC, Canada
    Posts
    2,233
    Articles
    57
    Blog Entries
    14
    Excel Version
    Excel Office 365 Insider
    Try this:

    Code:
    Sub SaveWorkflow()
    Dim sDest As String
    Dim sFolder As String
    Dim sUser As String
        sUser = Right(Environ("USERPROFILE"), Len(Environ("USERPROFILE")) - InStrRev(Environ("USERPROFILE"), "\"))
        Sheets("TabB").Visible = True
        Sheets("TabC").Visible = True
        Sheets("TabB").Cells.Copy
        Sheets("TabC").Cells.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("A1").Select
        Application.CutCopyMode = False
     
        Sheets("TabC").Copy
        sDest = Application.WorksheetFunction.Substitute( _
            Worksheets("Sheet1").Range("CA1").Value, _
            "1234567$", sUser)
        sFolder = Left(sDest, InStrRev(sDest, "\"))
        CreateFolder (sFolder)
        ActiveWorkbook.SaveAs sDest
     
        ActiveWorkbook.Close
     
        MsgBox ("You Can Now Reset")
     
    End Sub
    Ken Puls, FCPA, FCMA, MS MVP (Excel)

    Learn to Master Your Data at the Power Query Academy (the world's most comprehensive online Power Query training) or with my book M is for Data Monkey!

    Main Site: http://www.excelguru.ca -||- Blog: http://www.excelguru.ca/blog -||- Forums: http://www.excelguru.ca/forums
    Check out the Excelguru Facebook Fan Page -||- Follow Me on Twitter

    If you've been given VBA code (a macro) for your solution, but don't know where to put it, CLICK HERE.

  10. #10
    I am still having some problems, I am so sorry for being a pain.

    Would it be possible for me to send you the spreadsheet?

    Thanks
    Mark

Page 1 of 2 1 2 LastLast

Tags for this Thread

Posting Permissions

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