VBA: Unmerge cells in rows

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I know merged cells are bad, but the person I'm helping already has merged cells within their spreadsheet. Two questions...

1) I know thru VBA code how to unmerge a cell, but is there any way to count the number of cells that are merged and then copy the text to all of those cells. So example, if the first merged cell starts in R25, but goes thru x25. That's 7 cells. Can the VBA code determine there are 7 cells, copy the contents of R25 and then Resize by 7 and paste the results?

2) This is somewhat related. If the merged cells are white, can we skip that section and keep moving from left to right? Of course, once we hit the last column in the row, then we would start with the next row, row 26. All this would start with column L.

Thanks,
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
You can determine the number of cells in a merged area using something like:
VBA Code:
Range("L1").MergeArea.Cells.Count
If L1:R1 is merged, the result will be 7. You can then unmerge that region, and copy the value across those cells.
VBA Code:
Sub FryGirl()
Dim i as Long

i = Range("L1").MergeArea.Cells.Count
Range("L1").UnMerge
Range("L1").Resize(1, i).Value = Range("L1").Value
End Sub
To determine if a cell is merged, you can test using MergeCells, which returns True or False:
VBA Code:
Range("L1").MergeCells
 
Upvote 0
Solution

Forum statistics

Threads
1,214,788
Messages
6,121,588
Members
449,039
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top