• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course โ€“ Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Find and Unmerge Merged Cells with VBA

You are here: Home / Excel VBA / Find and Unmerge Merged Cells with VBA
find and unmerge merged cells using vba
April 10, 2019 by Philip Treacy

If you've worked with merged cells you may know that they can cause issues with things like copying and pasting, sorting, and counting cells.

Merged cells can cause VBA to fall over too, so it is best to avoid them. Using Center Across Selection is a better option.

CTRL+1 is the shortcut to open Format Cells -> Alignment and choose Center Across Selection from the Horizontal dropdown list.

center across selection

I've written a couple of simple VBA routines that will highlight any merged cells on the sheet, and you can run the other macro to unmerge any merged cells.

Download The Sample Workbook

Enter your email address below to download the sample workbook.



By submitting your email address you agree that we can email you our Excel newsletter.
Please enter a valid email address.
Download the Excel Workbook. Note: This is a .xlsm file please ensure your browser doesn't change the file extension on download.

Let's say we have a sheet like this that is used to create invoices.

invoice sheet

Running my routine highlights any merged cells which shows this

merged cells highlighted

We can then remove any merged cells and with a little bit of reformatting we end up with this invoice - and no merged cells in sight.

merged cells unmerged

The Code

The first macro searches the active sheet for merged cells and highlights them in green.

I've assigned the shortcut sequence CTRL+SHIFT+M to the macro and running it repeatedly toggles the highlighting on/off.

vba to find merged cells

If you want to unmerge the merged cells then press CTRL+SHIFT+U to run this macro

vba to unmerge merged cells

find and unmerge merged cells using vba

More Excel VBA Posts

Display All Matches from Search in Userform ListBox

Display All Matches from Search in Userform ListBox

Search a range for all partial and full matches of a string, and display matching records (entire rows) in a userform listbox. Sample code and userform.
animating excel charts

Animating Excel Charts

Use animation correctly to enhance the story your data is telling. Don't animate your chart just for some eye candy. Sample code and workbook to download.
dynamic data validation lists in userforms

Dynamic Data Validation Lists in Userforms

Data validation lists using the same source that are dynamically modified to prevent the same choice being made in each list.
show report filter pages for power pivot pivottables

Show Report Filter Pages for Power Pivot PivotTables

PivotTables created from Power Pivot can't use the 'Show Report Filter Pages' option. But this piece of VBA allows you to do just that.
charting real time data in excel

Charting Real Time Data in Excel

Receive data in real time and chart the data as it arrives. Can be used to chart things like stock prices or sensor readings. Sample code and workbook
select multiple items from drop down data validation list

Select Multiple Items from Drop Down (Data Validation) List

Choose multiple items from a data validation (drop down) list and store them all in the same cell. Sample workbook with working VBA.
Excel Calendar (Date Picker) to Use in Worksheets and Userforms

Multi-Language Excel Calendar (Date Picker) for Worksheets and Userforms

Easy to use, highly customizable and multi-language. This date picker is implemented as a userform that is simple to integrate into your workbook.
automating and emailing pivot table reports

Automating and Emailing Pivot Table Reports

Automate the creation and distribution of pivot table reports with some VBA. Send reports to multiple email recipients using Outlook.
search for data with userform

Searching for Data With a User Form

Search a list of records (like a table) using a user form, and then populate the fields of the search form when the record is found.
Checking values in range objects with vba

Checking Values in Range Objects With VBA

Use built in tools or your own code to examine the values contained within Range objects in VBA. Sample code to download.


Category: Excel VBA
Previous Post:Static variables in VBAStatic Variables in VBA
Next Post:Power Query Approximate Match VLOOKUPpower query approximate match vlookup

Reader Interactions

Comments

  1. YD Forums

    December 25, 2020 at 1:33 am

    I’m need to add a step to the process. After finding and unmerging cells, I want to fill the now empty cells with the contents of the previously merged cell. For example, if A1, A2, and A3 were merged and contained TEXT, I want to unmerge into individual A1, A2, and A3, and have each of them contain TEXT.

    Can you advise how to do that?

    Thanks

    Reply
    • Catalin Bombea

      December 25, 2020 at 2:32 am

      Hi,
      If there are merged cells, you can read the address of the merged area and use it to fill it after unmerge:

      Sub UnMergeAndFill()
      Dim cell As Range, MergeAddress As String
      For Each cell In ActiveSheet.UsedRange
      If cell.MergeCells = True Then
      MergeAddress = cell.MergeArea.Address
      Range(MergeAddress).UnMerge
      Range(MergeAddress).Value = Range(MergeAddress).Cells(1).Value
      End If
      Next
      End Sub

      When you unmerge cells, the value always goes into the first cell of the merged area, that’s what the code speculates.

      Reply
  2. John Mann

    May 29, 2020 at 5:45 am

    I’ve never understood why one would want to merge cells in a spreadsheet. I do use that ability sometimes in wordprocessor tables, but they have a different function. I long ago recorded macro in my personal workbook to perform the Centre Across Selection format, which I use quite often. It’s triggered by an icon in my QAT (which stretches almost 1/2 way across the screen on my laptop.

    Sub CentreAcrossCollumns()
    ‘
    ‘ CentreAcrossCollumns Macro
    ‘
    With Selection
    .HorizontalAlignment = xlCenterAcrossSelection
    .MergeCells = False
    End With
    End Sub

    Reply
    • Catalin Bombea

      May 30, 2020 at 1:13 am

      Hi John,
      If we try to understand everything that has no apparent reason, a lifetime will not be enough ๐Ÿ™‚
      Thanks for sharing
      Catalin

      Reply
  3. Rudra Sharma

    May 16, 2019 at 4:17 pm

    Very^99 simple code, exactly I had thought.

    Reply
  4. jim

    April 12, 2019 at 11:52 pm

    and don’t forget the final step

    find out who merged them in the first place and “re-educate” them

    jim

    Reply
  5. slawek

    April 12, 2019 at 3:25 pm

    It will be nice if use proper collection in for each loop instead of default object. Many you readers can be confused what this loop actually does.

    Reply
  6. mrsimlaoui

    April 12, 2019 at 7:48 am

    nice post. thank you.
    i have a suggestion without vba
    1- select the sheet
    2- ctrl+F
    3- Format
    4- in the list we choose format then alignement
    5- i activate the box befor merged cell
    6- serach all
    7- ctrl+A to select all cells
    8- change format (maybe add a color…)
    9- correct cell one by one

    thank you. but your way is better and faster.
    cheers.

    Reply
    • Philip Treacy

      April 12, 2019 at 8:59 am

      Thanks. There’s more than one way to skin a cat ๐Ÿ™‚

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Current ye@r *

Leave this field empty

Sidebar

More results...

launch excel macros course excel vba course

Featured Content

  • 10 Common Excel Mistakes to Avoid
  • Top Excel Functions for Data Analysts
  • Secrets to Building Excel Dashboards in Less Than 15 Minutes
  • Pro Excel Formula Writing Tips
  • Hidden Excel Double-Click Shortcuts
  • Top 10 Intermediate Excel Functions
  • 5 Pro Excel Dashboard Design Tips
  • 5 Excel SUM Function Tricks
  • 239 Excel Keyboard Shortcuts

100 Excel Tips and Tricks eBook

Download Free Tips & Tricks

Subscribe to Our Newsletter

Receive weekly tutorials on Excel, Power Query, Power Pivot, Power BI and More.

We respect your email privacy

239 Excel Keyboard Shortcuts

Download Free PDF

mynda treacy microsoft mvpHi, I'm Mynda Treacy and I run MOTH with my husband, Phil. Through our blog, webinars, YouTube channel and courses we hope we can help you learn Excel, Power Pivot and DAX, Power Query, Power BI, and Excel Dashboards.

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk – For Technical Issues

Copyright © 2023 ยท My Online Training Hub ยท All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.