gpt4 book ai didi

Excel 过滤合并单元格

转载 作者:行者123 更新时间:2023-12-02 16:00:45 25 4
gpt4 key购买 nike

我正在尝试计算每个员工在他所从事的每个项目上的工作时间。但我不知道如何选择,因为包含员工姓名的单元格如图所示合并。如果我想看看项目没有。 3 是参与工作的员工,Excel 筛选不能采用仅对应于项目 1 的姓名“John”。为了更清楚,我需要知道如何对单元格 A3 和 A4 进行过滤。如果我取消合并单元格,约翰将只在单元格 A2 上,事实上他也参与项目 2 和 3。

谢谢!

Excel screenshot

最佳答案

如果您有一个合并单元格,并且您尝试对其进行筛选,您将仅获得第一行:
Rows with Merged Cells
Filtered Merged Cells only shows first row

要解决此问题,您首先需要在其他位置创建合并单元格,取消合并过滤单元格,并将值填充到所有单元格中:
Table cells unmerged, merged cells on right

然后,您可以复制合并的单元格,然后在您想要合并的单元格上选择性粘贴 > 格式:
Copying the Merged Cells and using Paste Special to put format in data table
The Merged Cell formatting pasted in place

您现在可以删除临时合并单元格,并且在过滤时您将获得合并单元格的所有行:
enter image description here

 
{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

关于Excel 过滤合并单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49816515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com