Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Finding the Path to the Desktop.

Finding the Path to the Desktop

Written by Allen Wyatt (last updated September 9, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


1

Donald is writing a macro in which he needs to reference a user's desktop. However, the path to the desktop necessarily varies from system to system and user to user. He wonders what coding he can use to determine the path to the desktop regardless of system.

There are several ways to find the path to the desktop in VBA. One way is to call the Windows scripting host, in this manner:

Function GetDesktop() As String
    Dim oWSHShell As Object

    Set oWSHShell = CreateObject("WScript.Shell")
    GetDesktop = oWSHShell.SpecialFolders("Desktop")
    Set oWSHShell = Nothing
End Function

Note that this is a user-defined macro that you can use either from the worksheet or from another macro. The use from the worksheet would be as follows:

=GetDesktop()

Another way to determine the path to the desktop is to use the following line in your code:

sPath = Environ("USERPROFILE") & "\Desktop"

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8236) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Finding the Path to the Desktop.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Selecting Just the Word

Double-click on a word and Word selects it, plus some. It also selects any trailing spaces after the word. Here's a ...

Discover More

Saving Common Formulas

Got some formulas you slaved over and want to use in lots of workbooks? This tip presents some helpful ideas on how you ...

Discover More

First and Last Names in a Page Header

When you have a worksheet that includes a long list of names, you may want the first and last names on each page to ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Understanding the While...Wend Structure

Logical structures are important in programming, as they allow you to control how the programming statements are ...

Discover More

Updating Automatically when Opening Under Macro Control

If your workbook contains links, you are normally given the opportunity to update those links when you open the workbook. ...

Discover More

Finding Workbooks Containing Macros

Workbooks can contain macros, or not. It is entirely up to you whether they do or not, but at some future time you might ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 + 0?

2023-09-09 10:56:20

J. Woolley

My Excel Toolbox's NameOf function can return the value of an environment variable. For example, this cell formula will return the path to your desktop as indicated in the Tip:
=NameOf("UserProfile")&"\Desktop"
For more about the NameOf function, see my earliest comment here:
https://excelribbon.tips.net/T013432_Inserting_the_Workbook_Name.html
For more about environment variables, see my earliest comment here:
https://excelribbon.tips.net/T013227_Adjusting_a_Path_Based_on_System_and_User.html
My Excel Toolbox's ListSpecialFolders dynamic array function will return 7 Excel special folders plus 48 Windows special folders, including the desktop's path:
=ListSpecialFolders([SkipHeader])
Expect 2 columns (Acronym, Folder) and 55 rows plus the optional header row.
In older versions of Excel that do not support dynamic arrays, you can preselect an appropriate 2x55 (plus header) range and enter the formula as a CSE (Ctrl+Shift+Enter) array. Or you can use My Excel Toolbox's SpillArray function to simulate a dynamic array:
=SpillArray(ListSpecialFolders())
If you only want the path to your desktop, use the following cell formula:
=VLOOKUP("Windows MyDesktop",ListSpecialFolders(),2,FALSE)
See https://sites.google.com/view/MyExcelToolbox


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.