[英]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.