How to Automatically Assign an Excel Sensitivity Label with VBA
Question details
The user needs a way to use VBA to automatically apply a Microsoft Purview sensitivity label when saving an Excel workbook.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Running an automated VBA macro that saves an Excel file in an enterprise environment with mandatory sensitivity labeling.
- Observed behavior
- The macro execution pauses indefinitely at the sensitivity label security prompt, and standard methods like Application.DisplayAlerts = False fail to bypass it.
Ensure you have the correct Microsoft 365 permissions and have obtained the specific Sensitivity Label ID (GUID) from your IT or compliance administrator before modifying your VBA code.
Apply the Label Using the Workbook.SensitivityLabel Property
Use the built-in SensitivityLabel object in VBA to programmatically set the document's classification label before executing the save command.
Microsoft 365 provides the SensitivityLabel API to handle compliance prompts programmatically. This method requires a valid Label ID provided by your organization's Microsoft Purview compliance settings.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor in Excel.
Consult your IT administrator to get the exact GUID for the sensitivity label you wish to apply to the workbook.
In your macro code, before the save function, utilize ActiveWorkbook.SensitivityLabel.CreateLabelInfo() to create a label info object, and set its LabelId property to the GUID.
Call the SetLabel method on the ActiveWorkbook.SensitivityLabel object using your configured label info, and then run your ActiveWorkbook.Save command. The prompt will no longer appear.

Consult Developer Resources and Verify IT Policies
If programmatic label assignment fails, it may be blocked by strict organizational policies or lack of specific permissions.
Switch to WPS Office for Seamless Macro Execution
If complex Microsoft 365 compliance APIs and enterprise restrictions are hindering your personal or small business spreadsheet tasks, consider WPS Office. It provides a highly compatible environment for your macros and daily spreadsheet needs without the overhead of mandatory compliance prompts.

Frequently Asked Questions
Why doesn't Application.DisplayAlerts = False skip the sensitivity label prompt?
Microsoft Purview sensitivity prompts are security and compliance features. They are intentionally designed to bypass standard DisplayAlerts suppression in VBA to guarantee that corporate data classification policies are enforced.
Where can I find the Sensitivity Label ID for my VBA code?
The Label ID is a unique GUID assigned within your organization's Microsoft Purview compliance portal. You must contact your Microsoft 365 administrator or security team to obtain the correct GUID for your script.
Can I run macros with sensitivity labels on older versions of Excel?
Programmatic interaction with the SensitivityLabel object requires newer builds of Microsoft 365. Older perpetual versions of Excel (such as Office 2016 or 2019) may not natively support these specific VBA compliance properties.




