问题
I created a sheet with merged cells, but the value of the merged cells is only stored in the first cell. Is there anyway, to keep the same value in each of the cells, I need that for a formula I use. Thanks!
回答1:
In Excel 2003 this macro does the job:
Public Sub UnmergeAndFill()
With Selection
If .MergeCells Then
.MergeCells = False
Selection.Cells(1, 1).Copy
ActiveSheet.Paste 'Or PasteSpecial xlPasteFormulasAndNumberFormats
End If
End With
End Sub
Create the macro by
- pressing Alt-F11, Ctrl-R, menu
Insert/Module
, paste the code; - alternatively: Alt-F8, type a new name (
UnmergeAndFill
, e.g.), clickMake
orCreate
(? don't know the English button text)
Invoke the Macro by pressing Alt-F8, select it, Run
. Alternatively map it to a key
回答2:
I know this is a rather old question, but this is the first place I landed when looking for an answer, and the accepted answer did not help at all. I DID discover an excellent answer, however, on MrExcel which I thought was worth putting on this thread for the benefit of anyone else googling for an answer:
http://www.mrexcel.com/forum/general-excel-discussion-other-questions/487941-data-multiple-cells-within-merged-cell-possible.html
To save looking up the link, the answer is remarkably simple; if you merge cells using Excel Format Painter, rather than Merge Cells, it preserves the data/formulae 'underlying' the merged cells. You just need to create a temporary merged block of cells in the right format somewhere else, to use as a template for the Format Painter. You can delete them afterwards. One thing to watch out for, though, is having 'hidden' data like this can be a trap for the unwary, since editing the visible cell does not change the invisible ones.
回答3:
you can make a new column (or row) and apply this formula in first cell and drag it down:
I suppose in column A you have merged cells (for example A1:A3 & A5:A8 are merged).
Insert a column before column A
In A1 type:
=B1
Copy the formula below in A2 :
=IF(B2="",A1,B2)
Drag down the formula u typed in A2
In your formulas use the newly created column and after use you can hide it.
回答4:
You could use a custom VBA function that gives directly the value of the merged cell, no matter which one you select. In that case it is not necessary to duplicate the values.
- Switch to VBA view (Alt-F11)
- Create a new module via Insert > Module
- In your project, browse to the new module (you might want to give it a new name via the (name) property just under the explorer)
- Copy the following code in the module (pasting the code in ThisWorkbook will not work)
Code:
Option Explicit
Function GetMergedValue(location As Range)
If location.MergeCells = True Then
GetMergedValue = location.MergeArea(1, 1)
Else
GetMergedValue = location
End If
End Function
- You can now use the formula in excel
Code:
=GetMergedValue(A1)
Where A1 is a part of a merged cell.
回答5:
can you not store the actual values somewhere else? instead of the merged cell? and yes , use a formula to display the value on the merged cell.
回答6:
Assume column "A" has merged cells - put this in B1 and copy it to fill the rest of the column:
=IF(ISBLANK(A1);OFFSET(B1;-1;0);A1)
It checks if the cell to the left has a value if it has it returns its value, if not, it takes the value from the upper cell.
Note that it doesn't work for the empty cells. First fill empty cells in column "A" with something unique, like "(empty)" and replace it back with emptiness both in "A" and "B" after filling column "B".
回答7:
I improved on sehe's macro to do as many merged cells as you select.
Code:
Public Sub UnmergeAndFillMultiple()
Dim w As Range, m As Range, n As Range
For Each w In Selection.Cells
If w.MergeCells Then
Set n = w.MergeArea.Cells(1, 1)
Set m = w.MergeArea
w.MergeCells = False
n.Copy
m.PasteSpecial
End If
Next
End Sub
回答8:
Dim rowcnt As Long, i As Long
rowcnt = Cells(Rows.Count, "A").End(xlUp).Row
For i = rowcnt To 3 Step -1
With Cells(i, 1)
If .Value = Cells(i - 1, 1).Value Then
.Font.ColorIndex = 9
End If
End With
Next i
来源:https://stackoverflow.com/questions/6464265/how-to-keep-value-of-merged-cells-in-each-cell