The quickest way to create SharePoint folders from Excel is to use VBA macros that directly interact with SharePoint’s API. Excel VBA can generate new folders in SharePoint Online or on-premises SharePoint Server with minimal manual steps, saving hours of repetitive work. You’ll need your SharePoint site URL, credentials (if needed), and the SharePoint object model in VBA. This guide shows you a full, working solution—no guessing, just reliable automation.

What You'll Need

  • Microsoft Excel with VBA editor enabled (press Alt+F11 to open)
  • A SharePoint site URL (for SharePoint Online: https://yourdomain.sharepoint.com/sites/SiteName; for on-premises: https://yourdomain/sites/SiteName)
  • Either:
    • Service account credentials (email/password) with "Full Control" permissions, or
    • A personal access token (PAT) from Azure AD if using SharePoint Online
  • SharePoint object model installed in Excel (most modern Excel versions include it; for older versions, install the SharePoint Object Model for Excel)
  • Knowledge of basic SharePoint folder paths (e.g., Documents/ProjectName/Folders)

Step-by-Step: Create a SharePoint Folder Macro in Excel VBA

  1. Set up SharePoint connection
    1. Open Excel and press Alt+F11 to open the VBA editor.
    2. Insert a new module (Insert > Module) and paste the following code. Replace your_Email, your_Password, and your_SiteURL with your credentials and SharePoint URL:
          Dim sp As Object
          Dim spSite As Object
          Dim spFolder As Object
    
          Set sp = CreateObject("Scripting.Network")
          Set spSite = CreateObject("Scripting.Network").CreateObject("Microsoft.SharePoint.SPSite", "your_SiteURL")
    
          ' If using personal access token (PAT) instead of password:
          ' Use the following to initialize (uncomment and modify):
          ' Set spSite = CreateObject("Scripting.Network").CreateObject("Microsoft.SharePoint.SPSite", "your_SiteURL", "your_PAT")
          
    1. For SharePoint Online (PAT method recommended): - Generate a PAT here: Create PAT - Replace your_PAT in the code above.
  2. Define folder path and create the folder
    1. Add this code to create a new folder at a specified path (e.g., /sites/SiteName/Project/Reports):
          Dim folderPath As String
          folderPath = "/sites/SiteName/Project/Reports"
    
          ' Check if folder exists; if not, create it
          On Error Resume Next
          Set spFolder = spSite.Folders.Add(folderPath)
          If Err.Number <> 0 Then
              Debug.Print "Folder already exists: " & folderPath
          Else
              Debug.Print "Folder created: " & folderPath
          End If
          
  3. Add a macro to run from Excel
    1. In Excel, go to Developer > Macros and select CreateSharePointFolder.
    2. Run the macro from Excel using Ctrl+F8 to edit the macro, then F5 to test.
    3. If using a password, prompt the user for credentials with this helper function:
          Function GetCredentials() As String
              Dim inputBoxResult As Variant
              inputBoxResult = InputBox("Enter SharePoint email and password (separated by comma):", "Credentials")
              GetCredentials = inputBoxResult
          End Function
          
    1. Modify the code to use `inputBoxResult` instead of hardcoded values.
  • Test with a single folder
    1. Run the macro once to verify it works for the test folder (e.g., /sites/SiteName/TempTest).
    2. If you get an error like Access Denied, ensure your account has permissions to create folders in the site.
  • Common Mistakes & How to Fix Them

    • Error: "Operation is not allowed on a trust relationship"
      1. This occurs if your Excel session is not trusted with the SharePoint site. Solution:
        1. Close and reopen Excel as Administrator.
        2. Or add a reference to the SharePoint Object Model manually (right-click VBA project > References > Check "Microsoft SharePoint Types" or similar).
    • Macro fails with "Cannot resolve the object" error
      1. Verify the SharePoint URL is correct (must start with https:// for Online, http:// for on-premises).
      2. Check the site exists by visiting it in a browser.
    • Folder creation succeeds but no changes appear
      1. Wait 10–60 seconds for SharePoint to propagate changes. Clear cache or refresh the site in a browser.
      2. If SharePoint Server, ensure the site collection administrator has permissions to create folders.
    • Excel crashes during VBA execution
      1. Run the macro in Safe Mode (Excel > File > Options > Privacy Settings > Enable Add-ins).
      2. Disable other add-ins temporarily.

    Tips for Advanced Use

    • Loop through multiple folders Use a loop to create folders dynamically based on data in Excel:
          Dim folderList As Variant
          folderList = Array("/sites/SiteName/Project1/Folder1", "/sites/SiteName/Project1/Folder2")
      
          For Each path In folderList
              Set spFolder = spSite.Folders.Add(path)
          Next
          
    • Store credentials securely Avoid hardcoding passwords. Use Excel’s Application.WorksheetFunction to load from a hidden sheet:
          Function GetSecurePassword() As String
              Dim ws As Worksheet
              Set ws = ThisWorkbook.Sheets("Credentials")
              GetSecurePassword = ws.Range("Email").Value & "|" & ws.Range("Password").Value
          End Function
          
    • Log operations for auditing Add logging to track created folders:
          Debug.Print "Created folder: " & folderPath & ", Path: " & spFolder.Url
          
    • Use SharePoint REST API for older versions If the SharePoint Object Model isn’t available, use REST API calls via HTTP requests:
          Dim http As Object
          Set http = CreateObject("MSXML2.XMLHTTP")
          http.Open "POST", "https://yourdomain.sharepoint.com/_api/web/Lists/getbytitle('Documents')/Folders('ProjectName')", False
          http.setRequestHeader "Authorization", "Bearer " & your_PAT
          http.setRequestHeader "Accept", "application/json;odata=verbose"
          http.send
          

    Troubleshooting Failed Operations

    1. Sign in fails with "403 Forbidden"
      1. Check if your account is a member of the SharePoint site (use https://yourdomain.sharepoint.com/_layouts/15/userdisp.aspx to confirm).
      2. If using a PAT, verify it’s valid and not expired (check Azure AD PAT expiry).
      3. Ask a SharePoint administrator to grant "Create Folders" permissions.
    2. Folder path contains invalid characters
      1. SharePoint allows only letters, numbers, spaces, and symbols like _, ~, @, #, $.
      2. Replace special characters with underscores (e.g., "My Folder" becomes My_Folder).
    3. Macro runs but returns "No folders were found"
      1. Verify the site path is absolute (e.g., /sites/SiteName, not just SiteName).
      2. Check for typos in the folder path (e.g., /sites/SiteName/Project/Reports vs. /sites/SiteName/Project/Report).
    4. SharePoint Server 2019/2016 errors: "404 Not Found"
      1. Ensure the SharePoint farm is accessible from Excel’s network (firewall rules may block connections).
      2. Try using http:// instead of https:// in the URL.
      3. For on-premises, map the SharePoint site as a drive and use the mapped drive path (e.g., \\server\SharePoint\SiteName).

    Frequently Asked Questions

    How do I create a folder in SharePoint Online without a PAT?

    You cannot. SharePoint Online requires a PAT or service account credentials for automation. Use the PAT method (recommended) or switch to a service account with full permissions.

    Can I automate folder creation in SharePoint Server 2019 without admin rights?

    No. Non-admin users cannot create folders in SharePoint Server without explicit permissions. Ask your SharePoint administrator to add you to the Site Collection Administrators group.

    What if my Excel VBA macro times out when connecting to SharePoint?

    Timeouts often occur due to network issues. Increase the timeout in VBA: Set spSite = CreateObject("Microsoft.SharePoint.SPSite", "your_SiteURL", , 60) (60 = timeout in seconds). Alternatively, use HTTP requests with a higher timeout value.

    How do I delete a folder I just created in SharePoint?

    Add this code to delete the folder (after confirming it exists): spSite.Folders.Delete(folderPath). Note: Deleting a folder removes all files inside it unless you have special permissions.

    Is there a way to automate folder creation without VBA?

    Yes, use Power Automate (Microsoft Flow) with SharePoint connectors. Create a flow that runs on a schedule or triggers from Excel.

    Why does my macro create folders but they don’t appear in the UI?

    SharePoint may require a page refresh or cache clearing. Right-click the folder in the browser and choose Refresh Content. For on-premises, ensure the SharePoint service is running (check the SharePoint Central Administration console).