如何解决创建跨多个表的不同值列表
我制作了一个包含过去 2 年体育信息的 Excel 文档。这包括跨多个名为“跑步”、“骑行”、“力量”等的多个表格。
对于每项运动,我都喜欢记录与我一起训练的人,有时只提到一个名字,有时只提到两个名字,用逗号分隔(例如“John Doe,Jane Doe”)。
我想总结一下所有表格中的所有名字,以及每个名字被提及的次数(我和他们玩过多少次。
我曾尝试使用这个公式:
AuthenticationResponseGrant
但这在不同的工作表中不能很好地工作,并且不能将逗号分隔的名称分开。
解决方法
添加对 Microsoft Scripting Runtime 的引用(工具 -> 引用...)。
然后你可以写如下代码:
Dim dict As New Dictionary
dict.CompareMode = TextCompare ' Uniqueness of names will be case-insensitive:
' John Doe is equivalent to jOhN dOe
Dim sheets() As Worksheet
sheets = Array(Worksheets("Running"),Worksheets("Cycling"),Worksheets("Power"))
Dim sheet As Variant
For Each sheet In sheets
Dim cell As Range
For Each cell In sheet.UsedRange.Cells
Dim substrings() As String
substrings = Split(cell.Value,",") ' split cell contents on the commas
Dim substring As Variant
For Each substring In substrings
substring = Trim(substring) ' remove spaces before and after substring
If Len(substring) > 0 Then ' ignore empty substrings
If Not dict.Exists(substring) Then dict(substring) = 0
dict(substring) = dict(substring) + 1
End If
Next
Next
Next
Dim key As Variant
For Each key In dict.Keys
Debug.Print key,dict(key) ' prints the name,and the number of times
Next
(我最初的方法没有考虑到给定单元格中可能有多个名称。)
,使用两个表 - Table1
和 Table2
,如下面的 B 和 D 列所示。
首先用逗号分隔的分隔符将两个表连接在一起:TEXTJOIN(",TRUE,Table1[Name],Table2[Name])
然后交换 XML 标签的逗号:SUBSTITUTE(TEXTJOIN(",Table2[Name]),"</s><s>")
使用 FILTERXML
生成所有值的列表(显示在 F3:F12 中):=FILTERXML("<t><s>" & SUBSTITUTE(TEXTJOIN(","</s><s>") & "</s></t>","//s")
创建这些值的唯一列表(显示在 H3:H6 中):=UNIQUE(F3#)
计算值(显示在 I3:I6 中):=COUNTIF(F3#,H3#)
编辑:
使用您的表名和列名:
=FILTERXML("<t><s>" & SUBSTITUTE(TEXTJOIN(",Running[With],Cycling[With]),"//s")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。