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+F11to 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
- Set up SharePoint connection
- Open Excel and press
Alt+F11to open the VBA editor. - Insert a new module (Insert > Module) and paste the following code. Replace
your_Email,your_Password, andyour_SiteURLwith 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")- For SharePoint Online (PAT method recommended):
- Generate a PAT here: Create PAT
- Replace
your_PATin the code above.
- Open Excel and press
- Define folder path and create the folder
- 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 - Add this code to create a new folder at a specified path (e.g.,
- Add a macro to run from Excel
- In Excel, go to
Developer > Macrosand selectCreateSharePointFolder. - Run the macro from Excel using
Ctrl+F8to edit the macro, thenF5to test. - 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- Modify the code to use `inputBoxResult` instead of hardcoded values.
- In Excel, go to
- Run the macro once to verify it works for the test folder (e.g.,
/sites/SiteName/TempTest). - 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"
- This occurs if your Excel session is not trusted with the SharePoint site. Solution:
- Close and reopen Excel as Administrator.
- Or add a reference to the SharePoint Object Model manually (right-click VBA project > References > Check "Microsoft SharePoint Types" or similar).
- This occurs if your Excel session is not trusted with the SharePoint site. Solution:
- Macro fails with "Cannot resolve the object" error
- Verify the SharePoint URL is correct (must start with
https://for Online,http://for on-premises). - Check the site exists by visiting it in a browser.
- Verify the SharePoint URL is correct (must start with
- Folder creation succeeds but no changes appear
- Wait 10–60 seconds for SharePoint to propagate changes. Clear cache or refresh the site in a browser.
- If SharePoint Server, ensure the site collection administrator has permissions to create folders.
- Excel crashes during VBA execution
- Run the macro in
Safe Mode(Excel > File > Options > Privacy Settings > Enable Add-ins). - Disable other add-ins temporarily.
- Run the macro in
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.WorksheetFunctionto 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
- Sign in fails with "403 Forbidden"
- Check if your account is a member of the SharePoint site (use
https://yourdomain.sharepoint.com/_layouts/15/userdisp.aspxto confirm). - If using a PAT, verify it’s valid and not expired (check Azure AD PAT expiry).
- Ask a SharePoint administrator to grant "Create Folders" permissions.
- Check if your account is a member of the SharePoint site (use
- Folder path contains invalid characters
- SharePoint allows only letters, numbers, spaces, and symbols like
_, ~, @, #, $. - Replace special characters with underscores (e.g.,
"My Folder"becomesMy_Folder).
- SharePoint allows only letters, numbers, spaces, and symbols like
- Macro runs but returns "No folders were found"
- Verify the site path is absolute (e.g.,
/sites/SiteName, not justSiteName). - Check for typos in the folder path (e.g.,
/sites/SiteName/Project/Reportsvs./sites/SiteName/Project/Report).
- Verify the site path is absolute (e.g.,
- SharePoint Server 2019/2016 errors: "404 Not Found"
- Ensure the SharePoint farm is accessible from Excel’s network (firewall rules may block connections).
- Try using
http://instead ofhttps://in the URL. - 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).