Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
834 views
in Technique[技术] by (71.8m points)

vba - Unmerging excel rows, and duplicate data

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...