我创建了一个带有合并单元格的表格,但是合并单元格中的值只存储在第一个单元格中。是否有任何方法可以让每个单元格保留相同的值?因为我需要用到这个公式。谢谢!
我创建了一个带有合并单元格的表格,但是合并单元格中的值只存储在第一个单元格中。是否有任何方法可以让每个单元格保留相同的值?因为我需要用到这个公式。谢谢!
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
创建宏的方法:
插入/模块
, 粘贴代码;UnmergeAndFill
),点击 Make
或 Create
按钮(不知道英文按钮文本?)通过按下 Alt-F8 调用宏,选择它,然后点击 Run
。或者将其映射到一个键。
代码:
Option Explicit
Function GetMergedValue(location As Range)
If location.MergeCells = True Then
GetMergedValue = location.MergeArea(1, 1)
Else
GetMergedValue = location
End If
End Function
代码:
=GetMergedValue(A1)
A1是合并单元格的一部分。
您可以创建一个新列(或行)并在第一个单元格中应用此公式,然后将其向下拖动:
我假设您在A列中合并了单元格(例如A1:A3和A5:A8被合并了)。
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
你能不能把实际的值存储在其他地方,而不是合并的单元格里?然后使用公式在合并的单元格中显示该值。
=IF(ISBLANK(A1);OFFSET(B1;-1;0);A1)
它检查左边的单元格是否有值,如果有值,则返回其值;如果没有,则从上面的单元格获取值。
请注意,它不适用于空单元格。首先,在"A"列中填充空单元格,使用唯一的内容,例如“(empty)”,然后在填充"B"列之后将其替换为空白。
a = Cells(13,1)
会返回一个值,但是
a = Cells(14,1)
不会 "空"a = Cells(X,1).MergeArea(1, 1) '如果x=13到52将返回A13的值。
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
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