简体   繁体   English

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

[英]Is there a way in Excel to display a certain cell if another contains specific text?

Hi everyone i have an excel document which is a list of people and the activities these people do, like: 大家好,我有一个Excel文档,该文档列出了这些人及其从事的活动,例如:

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

I need to have a list of activities counting the number of people that do that activity and theyr names. 我需要有一个活动列表,其中包括进行该活动的人数和他们的名字。 For example: 例如:

 Programming 2 people David Lucy Swimming 2 people David Martin 

I know i accomplish with an IF function but i don't want blank spaces between names, so i need a function that: if the person does the activity the name is added but if not, it checks the next person. 我知道我用IF函数完成了操作,但是我不希望名称之间有空格,所以我需要一个函数:如果此人进行了活动,则会添加名称,但如果没有,它将检查下一个人。

What can i use? 我可以使用什么?

You seem fairly hell bent on the VBA approach and if that's the case, copy and paste the below code into a new module within your workbook ... 您似乎对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

... then create a sheet in your workbook called Transformed . ...然后在工作簿中创建一个名为Transformed的工作表。

Add a reference to the below library ... 添加对以下库的引用...

在此处输入图片说明

Now select your matrix of data and run the macro. 现在选择数据矩阵并运行宏。

在此处输入图片说明

I hope it works for you. 我希望这个对你有用。

暂无
暂无

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

相关问题 如果另一个单元格包含 Excel 中的特定文本,则在一个单元格中生成一个值 - Produce a value in one cell, if another cell contains a specific text in Excel Excel函数:如果单元格包含某些文本,则执行特定公式 - Excel function: IF a cell contains certain text, then a specific formula executes Excel-如果单元格包含特定文本 - Excel - If cell contains specific text Excel:如果单元格包含特定文本,则返回值 - Excel: Return value if cell contains a certain text Excel:如果单元格包含特定值,则将某些文本放入新单元格中 - Excel : If cell contains certain value then put certain text in new cell 如何将单元格文本与包含Excel中已有文本的另一个单元格合并? - How to join a cell text with another cell that contains text already in excel? 使用带有某些字母的Excel&VLOOKUP在单元格中显示计数 - Display Count in cell with Excel & VLOOKUP w/Contains for certain letters Excel VBA:当另一个单元格包含特定文本或字符串时,如何清除指定单元格的内容 - Excel VBA: How to clear contents for specified cells when another cell contains specific text or string 如果另一个单元格中的值包含特定文本,则显示消息框的VBA代码 - VBA code to display message box if value in another cell contains specific text Excel - 如果单元格包含列表中的文本,则返回另一个单元格的值 - Excel - if cell contains text in a list then return value of another cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM