How to allow merging cells within a protected worksheet in Excel?
In Excel, worksheets often need to be protected in order to prevent accidental changes or unauthorized editing. When a worksheet is protected, however, the ability to merge cells is typically disabled by default. This can be problematic when you need to format or reorganize your data—for example, when creating reports, aligning headers, or simplifying presentation—since merging cells within protected sheets is not directly supported through the standard Excel interface.
Fortunately, there are effective solutions that enable you to merge cells even when a worksheet is protected. In this article, we will guide you through a practical VBA code method to achieve this. We will also discuss relevant details, potential caveats, and provide additional tips for optimizing your workflow.
Allow merging cells within a protected worksheet with VBA code
Allow merging cells within a protected worksheet with VBA code
As Excel’s worksheet protection settings do not offer the option to merge cells, utilizing a VBA macro can help bypass this limitation by temporarily unprotecting the worksheet, merging the specified cells, and then reprotecting it with the same password. This approach is especially useful in situations where you need to quickly adjust the cell layout or organization on protected sheets, such as customizing invoice templates, forms, or project summaries, while maintaining an overall security policy for your data.
It is important to be cautious when executing VBA macros, especially in shared workbooks or those containing sensitive information. Always ensure you are comfortable with running macros and that you have saved your workbook beforehand, as unexpected changes cannot always be undone after macro execution.
Follow the instructions below to merge selected cells within a protected worksheet using VBA:
1. Begin by selecting the group of cells you wish to merge on your protected worksheet. Selecting the correct range at this stage helps prevent errors in the next steps.
2. Press the Alt + F11 keys together to open the Microsoft Visual Basic for Applications editor window. This environment allows you to insert and run custom macros within your workbook.
3. In the VBA editor, click on Insert > Module. This action creates a new module where you can enter your macro code. Copy and paste the VBA code provided below into the code window of this module.
VBA code: Allow merging cells within a protected worksheet
Sub MyMergeCells()
Application.ScreenUpdating = False
ActiveSheet.Unprotect "123"
Selection.Merge
ActiveSheet.Protect "123"
Application.ScreenUpdating = true
End Sub Note: In this sample code, the password is set as “123”. If your worksheet uses a different password, please replace “123” with the appropriate password to ensure the code functions correctly. Choosing a strong, memorable password is always recommended for protecting important data, and be sure to keep your password secure.

4. To execute the macro, press the F5 key or click on the Run button
in the VBA toolbar. A prompt may appear in Microsoft Excel confirming the action; click OK to continue. Your selected cells should then be merged as specified, even though the worksheet remains protected.

Once these steps are completed, your chosen range of cells will be merged immediately within the protected worksheet. This method enables quick formatting adjustments without compromising worksheet security.
Keep in mind that running this macro will temporarily unprotect and then reprotect the worksheet using the provided password. Be cautious to avoid misplacing or altering your protection password, as entering the wrong password or losing it may prevent access to your protected data. It is best practice to back up your workbook before applying VBA solutions to avoid accidental data loss. Troubleshooting tip: If the cells do not merge, double-check the selected range, ensure the password matches, and confirm that macros are enabled in your Excel settings (File > Options > Trust Center > Macro Settings).
If you prefer not to use VBA or need a solution for simpler scenarios (such as repeated merging), you can also consider unprotecting the worksheet temporarily via the Review tab, merging cells, and then protecting it again. However, this manual approach is less efficient and may not be suitable for repetitive tasks or larger workbooks. Another alternative is to adjust cell formatting with conditional formatting or alignment settings, though these do not physically merge cells but can mimic a merged appearance for display purposes.

Unlock Excel Magic with Kutools AI
- Smart Execution: Perform cell operations, analyze data, and create charts—all driven by simple commands.
- Custom Formulas: Generate tailored formulas to streamline your workflows.
- VBA Coding: Write and implement VBA code effortlessly.
- Formula Interpretation: Understand complex formulas with ease.
- Text Translation: Break language barriers within your spreadsheets.
Related articles:
- How to allow editing objects in a protected worksheet in Excel?
- How to allow external data refresh in a protected worksheet in Excel?
- How to allow spell check in a protected worksheet in Excel?
- How to protect cell formatting but only allow data entry in Excel?
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
All Kutools add-ins. One installer
Kutools for Office suite bundles add-ins for Excel, Word, Outlook & PowerPoint plus Office Tab Pro, which is ideal for teams working across Office apps.
- All-in-one suite — Excel, Word, Outlook & PowerPoint add-ins + Office Tab Pro
- One installer, one license — set up in minutes (MSI-ready)
- Works better together — streamlined productivity across Office apps
- 30-day full-featured trial — no registration, no credit card
- Best value — save vs buying individual add-in