如果另一个单元格包含特定文本,Excel中是否可以显示某个单元格?

时间:2019-04-20 07:25:30

标签: excel

大家好,我有一个Excel文档,该文档列出了人员及其从事的活动,例如:

People programmming swimming golf
David       Yes        Yes    No
Lucy        Yes        No     Yes
Martin      No         Yes    Yes

我需要有一个活动列表,其中包括进行该活动的人数和他们的名字。例如:

Programming 2 people
     David
     Lucy

Swimming 2 people
     David
     Martin

我知道我用IF函数完成了操作,但是我不希望名称之间有空格,因此我需要一个函数:如果此人进行了活动,则会添加名称,否则,它将检查下一个人员。 / p>

我可以使用什么?

1 个答案:

答案 0 :(得分:0)

您似乎对使用VBA的方法一无所知,如果是这样,请将下面的代码复制并粘贴到工作簿中的新模块中...

Option Explicit

Public Sub TransformData()
    Dim rngCells As Range, lngCol As Long, lngRow As Long, strHeader As String
    Dim lngWriteRow As Long, objDict As Scripting.Dictionary, arrNames() As String
    Dim objDestSheet As Worksheet, i As Long, x As Long

    Set objDestSheet = Worksheets("Transformed")
    Set objDict = New Scripting.Dictionary
    Set rngCells = Selection

    objDestSheet.Cells.Clear

    With rngCells
        For lngCol = 2 To .Columns.Count
            strHeader = .Cells(1, lngCol)

            ' Reset the array in case no names are found to have a yes next to them.
            ReDim Preserve arrNames(0)
            arrNames(0) = ""

            For lngRow = 2 To .Rows.Count
                If Left(UCase(.Cells(lngRow, lngCol)), 1) = "Y" Then
                    ReDim Preserve arrNames(UBound(arrNames) + 1)
                    arrNames(UBound(arrNames)) = .Cells(lngRow, 1)
                End If
            Next

            objDict.Add strHeader, arrNames
        Next
    End With

    With objDestSheet
        For i = 0 To objDict.Count - 1
            strHeader = objDict.Keys(i)
            arrNames = objDict.Items(i)

            strHeader = strHeader & " " & UBound(arrNames) & " people"

            lngWriteRow = lngWriteRow + 1
            .Cells(lngWriteRow, 1) = strHeader

            For x = 1 To UBound(arrNames)
                lngWriteRow = lngWriteRow + 1
                .Cells(lngWriteRow, 1) = arrNames(x)
            Next

            lngWriteRow = lngWriteRow + 1
        Next
    End With

    objDestSheet.Activate
End Sub

...,然后在工作簿中创建一个名为 Transformed 的工作表。

添加对以下库的引用...

enter image description here

现在选择数据矩阵并运行宏。

enter image description here

我希望它对您有用。