+ Reply to Thread
Results 1 to 12 of 12

Macro for saving a worksheet to desktop by any user of the file

  1. #1
    Registered User
    Join Date
    01-24-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Macro for saving a worksheet to desktop by any user of the file

    Hi,

    I have created a file that outputs a letter for a client.
    I then created a Macro that moves and copies the worksheet to a new Excel file and saves to my desktop and names the worksheet "Customer Copy" in excel.
    I also save this copied file as a PDF file same name "Customer copy".

    The macro works fine on my PC, the trouble I have is when I send this file to others users they get an error message when they try to run the Macro on their PC.
    I know the reason is that my Macro is linked to my user name (Hardeep.Sidhu)

    Below is my Macro:

    Sub Macro1()
    '
    ' Macro1 Macro
    '

    '
    Sheets("Output Summary").Select
    Sheets("Output Summary").Copy
    ChDir "C:\Users\hardeep.sidhu\Desktop"
    ActiveWorkbook.SaveAs Filename:= _
    "C:\Users\hardeep.sidhu\Desktop\Customercopy.xlsx", FileFormat:= _
    xlOpenXMLWorkbook, CreateBackup:=False
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:\Users\hardeep.sidhu\Desktop\Customercopy.pdf", Quality:=xlQualityStandard _
    , IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
    False
    ActiveWindow.Close
    End Sub



    The error users get in that macro is the line below:

    ChDir "C:\Users\hardeep.sidhu\Desktop"

    This links to my desktop, How can I change it so that it automatically go to the relevant users desktop?

    Please can anyone correct my Macro above, I am a novice at this stuff and have struggled for the last 7 hours.

    Thanks

    Hardeep

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: Macro for saving a worksheet to desktop by any user of the file

    Change this line
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    and all other cases where your name appears to Environ("Username")
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Macro for saving a worksheet to desktop by any user of the file

    Use the below code in your sub. The variable sDesktopPath will hold the Desktop path for the current user

    Please Login or Register  to view this content.
    Regards,
    Khaled Elshaer
    www.BIMcentre.com

    Remember To Do the Following....
    1. Thank those who have helped you by clicking the Star below their post.
    2. Mark your post SOLVED if it has been answered satisfactorily:
    • Select Thread Tools (on top of your 1st post)
    • Select Mark this thread as Solved

  4. #4
    Registered User
    Join Date
    01-24-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Macro for saving a worksheet to desktop by any user of the file

    Hi Alan,

    It is now not saving and I get the error message end/debug on the debugger the line:
    ChDir "C:\Users\Environ(Username)\Desktop"

    Is higlighted yellow.

  5. #5
    Registered User
    Join Date
    01-24-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Macro for saving a worksheet to desktop by any user of the file

    Hi Khaled,

    That is confusing where would I insert that in my original macro.

    Regards

    Hardeep

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: Macro for saving a worksheet to desktop by any user of the file

    Have you placed quotation marks around Username, ie Environ("Username") ?

    Alan

  7. #7
    Registered User
    Join Date
    01-24-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Macro for saving a worksheet to desktop by any user of the file

    When I do that is says
    Compile error: Expected end of statement

  8. #8
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: Macro for saving a worksheet to desktop by any user of the file

    Try this: "C:\Users\" & Environ("Username") &"\desktop\"

    Alan
    Last edited by alansidman; 01-25-2013 at 12:03 PM.

  9. #9
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Macro for saving a worksheet to desktop by any user of the file

    Here you Go.

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    01-24-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Macro for saving a worksheet to desktop by any user of the file

    Thank you so much everyone.
    I have used Khaled's last post and it works brilliant on mine and my colleagues PC, perfect.
    The only downside is I don't understand what I have done. I will try to figure this out for future scenarios.

    Many Thanks everyone.
    Last edited by Hardup; 01-25-2013 at 12:14 PM.

  11. #11
    Registered User
    Join Date
    10-30-2014
    Location
    Saudi Arabia
    MS-Off Ver
    2007
    Posts
    4

    Re: Macro for saving a worksheet to desktop by any user of the file

    Quote Originally Posted by Kelshaer View Post
    Here you Go.

    Please Login or Register  to view this content.
    Hello

    Sorry for necro-ing an old thread, but this appears to be exactly what I need!

    However, I am trying to change the name in this macro from "Customercopy" to whatever is present in my H1 cell. I can't seem to figure this out...

    So far, I have been using this simple macro for my day to day needs, but this won't work if I send it to a different pc:

    Sub SaveByCell()
    Dim sFileName As String
    sFileName = Sheets("Sheet1").Range("H1")
    If sFileName = "" Then Exit Sub
    ThisWorkbook.SaveAs "E:\Users\MyUserName\Desktop\Autosaves\" & sFileName
    End Sub

    Any ideas? Thanks!

  12. #12
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,602

    Re: Macro for saving a worksheet to desktop by any user of the file

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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