Results 1 to 10 of 12

Thread: MACRO - Check/Create Folder Structure And Save

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Post MACRO - Check/Create Folder Structure And Save

    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,450
    Articles
    44
    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

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

    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
    Super Moderator Bob Phillips's Avatar
    Join Date
    Mar 2011
    Posts
    1,852
    Articles
    0
    Excel Version
    O365
    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
    Super Moderator Bob Phillips's Avatar
    Join Date
    Mar 2011
    Posts
    1,852
    Articles
    0
    Excel Version
    O365
    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

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
  •