To set a sensitivity label in Microsoft Excel using VBA, you must use the SensitivityLabel property of the Workbook object, specifically utilizing the SetLabel method. Because sensitivity labels are part of the Microsoft Purview Information Protection framework, this requires a subscription that supports these labels and a version of Office that integrates with the Microsoft 365 compliance center. Automating this process ensures that every file generated by a macro is automatically classified, removing the risk of human error and ensuring that sensitive corporate data is encrypted or tagged according to organizational policy.
What You'll Need
- A Microsoft 365 subscription with a license that includes Microsoft Purview Information Protection (e.g., E3 or E5).
- An active sensitivity label policy published to your account by your organization's IT administrator.
- The Label ID (a unique GUID) for the specific label you wish to apply. This is not the display name (like "Confidential"), but a long string of alphanumeric characters.
- Excel for Microsoft 365 (Desktop version) with the Developer tab enabled.
Step-by-Step
- Enable the Developer Tab: If you have not already done so, right-click any tab on the Ribbon, select "Customize the Ribbon," and check the box for "Developer" in the right-hand column. Click OK.
- Open the VBA Editor: Click the Developer tab and select "Visual Basic" (or press Alt + F11).
- Insert a New Module: In the Project Explorer on the left, right-click your workbook name, select "Insert," and then choose "Module." This provides a clean space to write your automation code.
- Obtain the Label ID: You cannot simply type "Secret" into the code. You need the Label ID. The most reliable way to find this is to manually apply a label to a workbook and then run a small piece of diagnostic code to print the ID to the Immediate Window. Use this snippet:
Debug.Print ActiveWorkbook.SensitivityLabel.GetLabel().LabelId. Note this ID down exactly. - Write the SetLabel Procedure: In your new module, create a subroutine. You will use the
SensitivityLabel.SetLabelmethod. This method requires aLabelInfoobject. Here is the structured approach for the code:Sub ApplySensitivityLabel() Dim labelId As String Dim labelInfo As SensitivityLabel.LabelInfo ' Replace the string below with your actual Label GUID labelId = "your-label-guid-here" ' Create the LabelInfo object Set labelInfo = ActiveWorkbook.SensitivityLabel.CreateLabelInfo() labelInfo.LabelId = labelId ' Apply the label to the workbook ActiveWorkbook.SensitivityLabel.SetLabel labelInfo, "Reason for change" End Sub - Define the Justification: The second argument in
SetLabelis the justification string. If your organization's policy requires a reason for downgrading a label (e.g., moving from "Highly Confidential" to "General"), this string is mandatory. If you are upgrading a label, this can often be left as an empty string"". - Run and Test: Press F5 while your cursor is inside the subroutine. Check the "Sensitivity" button on the Home tab of the Excel Ribbon to verify that the correct label is now highlighted.
- Save as Macro-Enabled Workbook: Go to File > Save As and ensure you select "Excel Macro-Enabled Workbook (.xlsm)" so the VBA code is preserved.
Common Mistakes
The most frequent error is attempting to use the label's display name (e.g., "Public") instead of the Label ID. The VBA engine does not recognize the human-readable name because labels can be renamed by administrators without changing the underlying ID. If you use the display name, the code will fail or simply do nothing.
Another common mistake is ignoring the justification requirement. Many corporate policies are configured to trigger a prompt when a user changes a label to a less restrictive one. When automating this via VBA, the SetLabel method will throw an error if the justification argument is omitted or left blank when the policy mandates a reason for the change.
Users also often forget that sensitivity labels are tied to the user's identity. If you share a macro-enabled workbook with a colleague who does not have the same permissions or is not part of the same label policy, the code may fail on their machine because they lack the authority to apply that specific Label ID.
Tips
To make your code more maintainable, avoid hard-coding the Label ID directly inside every subroutine. Instead, create a global constant at the top of your module (e.g., Public Const CONFIDENTIAL_LABEL_ID As String = "..."). This allows you to update the ID in one place if the organization migrates to a new labeling schema.
For a more professional implementation, wrap your SetLabel call in an On Error Resume Next block or a proper error handler. This prevents the entire macro from crashing if the user is offline or if the Microsoft Purview service is momentarily unavailable, which can happen in unstable network environments.
If you are building a tool for a large team, consider creating a mapping function. You can create a custom function that takes a friendly name (like "Internal") and returns the corresponding GUID. This makes the code much easier for other developers to read and audit.
Troubleshooting
If the label is not appearing after running the code, first check your internet connection. Sensitivity labels require a handshake with the Microsoft 365 cloud to validate the label's existence and the user's permissions. If you are working offline, the label may not apply correctly.
If you receive a "Method SetLabel of object SensitivityLabel failed" error, verify the Label ID. A single missing character in the GUID will cause the method to fail. Re-run the diagnostic Debug.Print code mentioned in the step-by-step section to ensure you have the exact ID for the current version of the label.
Check if the workbook is in "Protected View." If you have opened a file from the internet or an email, Excel may block VBA from modifying the file's properties until you click "Enable Editing." Ensure the workbook is fully editable before the macro attempts to set the label.
If the code runs without error but the label doesn't change, check with your IT administrator to see if there is a "mandatory labeling" policy. In some strict environments, certain labels cannot be overridden by scripts if they conflict with a higher-level policy applied to the folder or SharePoint site where the file is stored.
Frequently Asked Questions
Can I remove a sensitivity label using VBA?
Yes, you can remove a label by using the SetLabel method and passing a LabelInfo object where the LabelId is set to an empty string or null, depending on your specific Office build's requirements. However, some organizational policies prevent the removal of labels entirely.
Does this work on Excel for Mac?
The SensitivityLabel object is primarily supported in the Windows version of Excel for Microsoft 365. While Mac versions of Office support labels manually, the VBA object model for sensitivity labels is often limited or unavailable on macOS.
Can I apply different labels to different sheets?
No. Sensitivity labels are applied at the workbook level (the file level), not the worksheet level. The encryption and classification apply to the entire .xlsx or .xlsm file.
Will the label persist if I save the file as a CSV?
No. CSV files are plain text and do not support metadata, encryption, or sensitivity labels. If you save a labeled workbook as a CSV, all sensitivity information is stripped away. You must save as .xlsx, .xlsm, or .xlsb to maintain the label.
How do I find the Label ID if I don't have a labeled file?
If you cannot manually label a file to find the ID, you will need to contact your Microsoft 365 Global Administrator or Compliance Administrator. They can find the Label GUIDs within the Microsoft Purview compliance portal under the Information Protection settings.