Reputation: 663
I have been given a fairly large database stored in Microsoft Excel, which I have to try convert into something useful.
However, one of the problems that I am encountering is that some of the data is merged together (horizontally in 2s).
For example;
row 1: [ x ][ x ][ x ][ x ][ x ]
row 2: [ x ][ x ][ o o ][ x ]
row 3: [ o o ][ x ][ o o ]
Where x's are single cells and o's are merged together
What I want to do is unmerge all the rows (which I could do fairly easy with the unmerge button), but for where the merged cell was, have the data duplicated across the 2 cells.
From; [[ Some Data ]]
To; [ Some Data ][ Some Data ]
Thanks! Any help is appreciated.
Upvotes: 37
Views: 82170
Reputation: 928
This is based on @aevanko 's answer, but generalized to an arbitrary Range.
Sub UnMergeRange(WorkRng As Range)
Dim cell As Range, joinedCells As Range
For Each cell In WorkRng
If cell.MergeCells Then
Set joinedCells = cell.MergeArea
cell.MergeCells = False
joinedCells.Value = joinedCells.Cells(1, 1).Value
End If
Next
End Sub
I also made a change because I came upon a corner case where it didn't work. It's when there's a vertically merged cell that includes cells above the provided range. I had to use a slightly more advanced expression to get the value.
Upvotes: 1
Reputation: 1
here is another way to have data in all cells. I have a report that is published daily with merged cells, to avoid daily unmerging cells I refer to report with Power Query.
using Fill down option I can populate data to all cells in the merged range. Works for me! :)
Report view with marked FIll Down option
Upvotes: 0
Reputation: 1
I don't know how to quickly implement the VBA code, but I used a formula to populate another column with the values... then I did the copy/paste magic of pasting values over the unmerged cells. This formula is based on the fact that the value is associated with the FIRST cell of the merged cells, and all others resolve to 0 (zero).
If Col. A2:A100 has the merged cells, I did the following:
=IF(A2<>0,A2, B1)
Actually, you can simplify it to this series if you're bold...
=IF(A2<>0,A2, B1)
That skips the use of Column C...
Upvotes: 0
Reputation: 6679
Upvotes: 20
Reputation: 2694
You don't need VBA for something like that.
If R1C1 styles are enabled from: File -> Options -> Formula -> R1C1 Reference style then
Upvotes: 3
Reputation: 14685
This is a VBA solution. This macro will search every cell in the active sheet to see if they are merged. If they are, it stores the range of the merged cells in a temp. range variable, unmerges the cells, then fills the range with the value of the first cell in the unmerged range (what the value was).
Sub UnMergeFill()
Dim cell As Range, joinedCells As Range
For Each cell In ThisWorkbook.ActiveSheet.UsedRange
If cell.MergeCells Then
Set joinedCells = cell.MergeArea
cell.MergeCells = False
joinedCells.Value = cell.Value
End If
Next
End Sub
Upvotes: 66