Microsoft Developer Network > Forums Home > Microsoft ISV Community Center Forums > Visual Basic for Applications (VBA) > How can I save a file in muli directories based upon the beginning of a directory path that is found in a worksheet cell? If Else and SaveAs
Ask a questionAsk a question
 

QuestionHow can I save a file in muli directories based upon the beginning of a directory path that is found in a worksheet cell? If Else and SaveAs

  • Friday, November 06, 2009 12:33 PMLT_Lynn Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    My users need to be able to save a file to a specific drive mapping or they must save to specific directories for an automated process.  I have a macro that will help define that path so they do not have to worry about typing.

    thus for an automated process they may need to go:  \\bpftp1\Fulfillment ftp\Data Loads\ProdTrans\130\ and \\bpftp1\Fulfillment ftp\Data Loads\StgTrans\130\  but for a manual process only to the C:\ drive.  I am defining the path in a field on a specific sheet.

    I want it so if it begins with \\bpftp1\Fulfillment ftp\Data Loads, the remainder of the path will be in the macro and it will save to multiple places.  But if it does not equal \\bpftp1\Fulfillment ftp\Data Loads then it should grab what is in that defined path field and save it there with the filename that is also defined.


    With ActiveWorkbook
    fname = .Worksheets("Macro").Range("D3").Value & ".csv"
    Path = .Worksheets("Macro").Range("D4").Value
    .SaveAs "\\bpftp1\Fulfillment ftp\Data Loads\ProdTrans\130\" & fname
    .SaveAs "\\bpftp1\Fulfillment ftp\Data Loads\StgTrans\130\" & fname
    .SaveAs "\\bpftp1\Fulfillment ftp\Data Loads\DevTrans\730\" & fname
    .SaveAs "\\bpftp1\Fulfillment ftp\Data Loads\DevConfig\680\" & fname
    End With
      
        ActiveWorkbook.Save
        ActiveWorkbook.Close
        Sheets("Macro").Select
    End Sub

    I was going to use an If Else statement:

    With ActiveWorkbook
    fname = .Worksheets("Macro").Range("D3").Value & ".csv"
    Path = .Worksheets("Macro").Range("D4").Value
    If Path = "\\bpftp1\Fulfillment ftp\Data Loads\" Then
      .SaveAs Path & "ProdTrans\130\" & fname
      .SaveAs Path & "StgTrans\130\" & fname
      .SaveAs Path & "DevTrans\730\" & fname
      .SaveAs Path & "DevConfig\680\" & fname
    Else
      .SaveAs Path & fname
    End If
    End With
      
    It does not like the \ in the path.