简体   繁体   English

Excel 过滤合并单元格

[英]Excel filtering for merged cells

I'm trying to make a calculation of hours worked for each employee on each project that he worked on.我正在尝试计算每个员工在他从事的每个项目中的工作时间。 But i don't know how to select because the cells that are containing the name of the employee are merged like in the picture.但我不知道如何 select 因为包含员工姓名的单元格像图片中一样合并。 And if i want to see on project no.如果我想看项目编号。 3 which are the employees that worked on, the Excel Filtering can't take the name "John" which corresponds only to project no.1. 3 是工作的员工,Excel 过滤不能取名称“John”,仅对应于 1 号项目。 To be more clear, I need to know how the filtering will be made for cells A3 and A4.为了更清楚,我需要知道如何对单元格 A3 和 A4 进行过滤。 If i will unmerge the cells, John will be only on cell A2, and in fact he worked also on projects 2 & 3.如果我取消合并单元格,John 将只在单元格 A2 上,事实上他也在项目 2 和 3 上工作。

Thanks!谢谢!

Excel 截图

If you have a Merged Cell, and you attempt to Filter for it, you will only get the first row:如果您有一个合并单元格,并且您尝试对其进行过滤,您将只会得到第一行:
合并单元格的行
过滤后的合并单元格仅显示第一行

To fix this, you first need to start by creating your Merged Cells somewhere else, unmerge your filter-cells, and fill the values into all cells:要解决此问题,您首先需要在其他地方创建合并单元格,取消合并过滤器单元格,然后将值填充到所有单元格中:
表格单元格未合并,右侧合并单元格

Then, you can Copy the merged cells, and Paste Special > Formats over the cells you want to merge:然后,您可以复制合并单元格,并粘贴>格式在合并的单元格:
复制合并的单元格并使用选择性粘贴将格式放入数据表中
合并单元格格式粘贴到位

You can now delete your temporary merged cells, and when you filter you will get all rows for the merged cell:您现在可以删除临时合并单元格,当您过滤时,您将获得合并单元格的所有行:
在此处输入图片说明


{EDIT} Here is a macro that will automatically apply the changes above to a specified range: {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

One other option;另一种选择;

Conditional formatting background for all cells with a particular value (eg John, red background) and then filter by colour.具有特定值(例如约翰,红色背景)的所有单元格的条件格式背景,然后按颜色过滤。

Wouldn't suit all applications but nice and quick.不适合所有应用程序,但又好又快。

The green tick answer by Chronocidal requires use of Macro. Chronocidal 的绿色勾号答案需要使用宏。 Personally, I don't think it is necessary to go through such cumbersome process.个人认为go没有必要通过这么繁琐的流程。

Suppose merged cells are in Column A;假设合并的单元格在 A 列中; first merged data is in cell A2.第一个合并的数据在单元格 A2 中。 Make a new column on the right of merged cell column [B Column].在合并的单元格列 [B Column] 的右侧创建一个新列。 In the cell B2, type =IF(LEN(A2)=0,B1,A2) and drag this formula down.在单元格 B2 中,键入 =IF(LEN(A2)=0,B1,A2) 并将此公式向下拖动。 It will fill data correctly and you can apply filter on column B.它将正确填充数据,您可以在 B 列上应用过滤器。

Note: Formula checks length of characters of left cell.注意:公式检查左侧单元格的字符长度。 If it is zero, uses cell directly above as answer;如果为零,则使用正上方的单元格作为答案; otherwise uses left cell as is.否则按原样使用左单元格。

Excel Snapshot of Formula Excel 公式快照

Use Filter . 使用Filter It is smart enough to deal with the merged cells. 处理合并的单元格足够聪明。 Eg, if you have a filter like this: 例如,如果您有这样的过滤器:

在此输入图像描述

and you filter projects 2, 3 and 4 then you get this: 并筛选项目2,3和4,然后你得到这个:

在此输入图像描述

which is quite ok. 这很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM