+ Reply to Thread
Results 1 to 4 of 4

save file in unknown default location with string?

Hybrid View

  1. #1
    Registered User
    Join Date
    06-19-2007
    Posts
    7

    save file in unknown default location with string?

    This one has me scratching my head...

    I need my macro to save the workbook in its default location each time it runs. I'm doing this with:
    Application.DisplayAlerts = False/True
    at the beginning and end of my routine. The file simply saves where it already is, and my macro doesn't need to know what its name is. The problem is:

    I want the macro to save two versions of the spreadsheet. The first as it already is, and the second (after a small manipulation in my routine) as the same filename & directory with a string " - string".xls tacked on the end of the filename.

    e.g. directory/filename" - string".xls

    How do I do this without explicitly stating the filename or file directory at some point within my macro?

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    You cann't

    You have to tell Excel where to save to or it will use your default location, which for most people is My Documents & you also need to give it the saveas filename

    You can use code to find out the path of the existing workbook & workbook name & have your code modify them as required

    Here is 2 versions
    Version 1 leaves you the workbook open that has string in its name
    Version2 leaves you with the workbook open with the same name before you saved

    Sub SaveVersion1()
       Dim sPath As String
       Dim sFname As String
       
       sPath = ThisWorkbook.Path
       sFname = ThisWorkbook.Name
       
       ThisWorkbook.Save
       ThisWorkbook.SaveAs Filename:=sPath & "\" _
          & Left(sFname, Len(sFname) - 4) & " String.xls"
    End Sub
    
    Sub SaveVersion2()
       Dim sPath As String
       Dim sFname As String
       
       sPath = ThisWorkbook.Path
       sFname = ThisWorkbook.Name
       
       ThisWorkbook.SaveAs Filename:=sPath & "\" _
          & Left(sFname, Len(sFname) - 4) & " String.xls"
       ThisWorkbook.SaveAs Filename:=sPath & "\" & sFname
    End Sub
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Registered User
    Join Date
    06-19-2007
    Posts
    7

    Smile great!..but a new problem arises:

    This is exactly what I was looking for, but when I execute my macro, it resaves my PERSONAL.XLS file as PERSONAL - string.xls
    I ran it a few times to test and now my macro workbook looks like PERSONAL - string - string - string - string.xls
    How do I make sure it is not taking this as my default filepath rather than the intended file?

  4. #4
    Registered User
    Join Date
    06-19-2007
    Posts
    7

    oh wait, I think I got it

    I changed any "ThisWorkbook" statement to an "ActiveWorkbook" statement and the problem was solved. Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1