如果您有一个合并的单元格,并且您尝试对其进行过滤,您只会得到第一行:
要解决此问题,您首先需要在其他地方创建合并单元格,取消合并过滤单元格,然后将值填充到所有单元格中:
然后,您可以复制合并的单元格,然后在您希望合并的单元格上粘贴“选择性粘贴”>格式:
您现在可以删除临时合并的单元格,当您过滤时,您将获得合并单元格的所有行:
{EDIT} 这是一个宏,它会自动将上述更改应用到指定范围:
Public Sub FilterableMergedCells()
Dim WorkingRange As Range
SelectRange:
Set WorkingRange = Nothing
On Error Resume Next
Set WorkingRange = Application.InputBox("Select a range", "Get Range", Type:=8)
On Error GoTo 0
'If you click Cancel
If WorkingRange Is Nothing Then Exit Sub
'If you select multiple Ranges
If WorkingRange.Areas.Count > 1 Then
MsgBox "Please select 1 continuous range only", vbCritical
GoTo SelectRange
End If
Dim ScreenUpdating As Boolean, DisplayAlerts As Boolean, Calculation As XlCalculation
ScreenUpdating = Application.ScreenUpdating
DisplayAlerts = Application.DisplayAlerts
Calculation = Application.Calculation
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Dim WorkingCell As Range, MergeCell As Range, MergeRange As Range, OffsetX As Long, OffsetY As Long
OffsetX = WorkingRange.Cells(1, 1).Column - 1
OffsetY = WorkingRange.Cells(1, 1).Row - 1
'Create temporary sheet to work with
With Worksheets.Add
WorkingRange.Copy .Cells(1, 1)
'Loop through cells in Range
For Each WorkingCell In WorkingRange.Cells
'If is a merged cell
If WorkingCell.MergeCells Then
'If is the top/left merged cell in a range
If Not Intersect(WorkingCell, WorkingCell.MergeArea.Cells(1, 1)) Is Nothing Then
Set MergeRange = WorkingCell.MergeArea
'Unmerge cells
MergeRange.MergeCells = False
'Replicate value to all cells in formerly merged area
For Each MergeCell In MergeRange.Cells
If WorkingCell.FormulaArray = vbNull Then
MergeCell.Formula = WorkingCell.Formula
Else
MergeCell.FormulaArray = WorkingCell.FormulaArray
End If
Next MergeCell
'Copy merge-formatting over old Merged area
.Cells(WorkingCell.Row - OffsetY, WorkingCell.Column - OffsetX).MergeArea.Copy
WorkingCell.PasteSpecial xlPasteFormats
End If
End If
Next WorkingCell
.Delete
End With
Set MergeRange = Nothing
Set WorkingRange = Nothing
Application.ScreenUpdating = ScreenUpdating
Application.DisplayAlerts = DisplayAlerts
Application.Calculation = Calculation
End Sub