logo
search
VBA & Macro Problems

How to Automatically Assign an Excel Sensitivity Label with VBA

Adam DavisAdam Davis Oct 9, 2026 869 views

Question details

The user needs a way to use VBA to automatically apply a Microsoft Purview sensitivity label when saving an Excel workbook.

How to Automatically Assign an Excel Sensitivity Label using VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor in Excel.

2
Obtain the Label ID

Consult your IT administrator to get the exact GUID for the sensitivity label you wish to apply to the workbook.

3
Create the Label Information

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.

4
Set the Label and Save

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.

Apply the Label Using the Workbook.SensitivityLabel Property
API Availability: The ability to set sensitivity labels programmatically depends strictly on your Microsoft 365 version, tenant labeling configuration, and organizational policy.
Free Microsoft Office alternative

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.

High compatibility with Microsoft Excel formats (.xlsx, .xls, .csv, .xlsm).Excellent support for standard VBA and macros to automate your spreadsheet tasks seamlessly.Free, lightweight, and fast-loading alternative to Microsoft Office.Familiar user interface requiring zero learning curve for existing Excel users.
microsoft office alternative - wps office

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.