+ Reply to Thread
Results 1 to 7 of 7

save to desktop macro

  1. #1
    Registered User
    Join Date
    11-07-2008
    Location
    PA
    Posts
    33

    save to desktop macro

    i currently have a macro that saves a file to a location specified in a ceratin cell
    Sub Button989_Click()

    ActiveWorkbook.SaveAs Filename:=Range("C37") & Range("C36").Value & ".xls"
    End Sub
    I need to do couple of things.

    1. get the above macro to save to desktop without having to insert the path...
    2. i want to give the option of saving it on the desktop or a folder in the c drive

    the problems

    how do i get excel to read the comptuer username to do the save as on the desktop

    ALSO how do i get excel to make a folder if the folder on the c drive does not exist

    is this even possible?

    Thank you

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good evening asabti
    Quote Originally Posted by asabti View Post
    how do i get excel to read the comptuer username to do the save as on the desktop
    This will return the path of the local computer's desktop :
    Please Login or Register  to view this content.
    Quote Originally Posted by asabti View Post
    ALSO how do i get excel to make a folder if the folder on the c drive does not exist
    This code will check for the existence of a directory called "Test" in the path "C:\" - if one isn't found it will be created :
    Please Login or Register  to view this content.
    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  3. #3
    Registered User
    Join Date
    11-07-2008
    Location
    PA
    Posts
    33
    this is the code i'm currently running its a hybrid between the code you provided and some other code: when i run it it says "subscript out of range" basically i have a drop down menu saying desktop or C:\Sample Spec Sheets

    Sub Button989_Click()
    MyDesktop = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
    With Worksheets("Sheet8")
    If .Range("C27").Value = "Desktop" Then
    ActiveWorkbook.SaveAs Filename:=MyDesktop & Range("C36").Value & ".xls"
    End If
    If .Range("C27").Value = "C:\Sample Spec Sheets" Then
    On Error Resume Next
    Set a = CreateObject("Scripting.FileSystemObject").getfolder("C:\Sample Spec Sheets")
    If Err = 0 = 0 Then
    MkDir "C:\Sample Spec Sheets"
    End If
    ActiveWorkbook.SaveAs Filename:="C:\Sample Spec Sheets" & Range("C36").Value & ".xls"
    End If
    End With
    End Sub

  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    You have several things to do if you want to prevent errors. To see which line is causing the error, in the VBE, press F8 in the routine until you find the error line.

    I considered a few problems here. Of course it will still error if C36 contains an illegal filename. You would need an API routine to check for a valid filename string. Same error occurs for illegal folder names in C27.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    11-07-2008
    Location
    PA
    Posts
    33
    when i press F8 to see where the error is.. it highlights this:

    Please Login or Register  to view this content.
    what is wrong herE?
    Last edited by VBA Noob; 11-20-2008 at 04:14 PM. Reason: Added code tags as per forum rules

  6. #6
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    Maybe a subscript error? This will happen if you don't have a sheet named "Sheet8".

    ActiveSheet should work ok if that is the sheet that you want to check.

  7. #7
    Registered User
    Join Date
    11-07-2008
    Location
    PA
    Posts
    33
    amazing! thank you for all your help!!! WOW 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