计算 Excel 电子表格中满足特定条件的唯一行数

发布于 2024-09-18 06:20:18 字数 254 浏览 6 评论 0原文

我想计算 Excel 列中包含特定字符串的所有唯一值。例如,假设我的专栏中有这个:

strA
strB
斯特拉
字符串
差异
差异B
strC

如果我给出这个理论函数,我正在写一个值“str”,它将计算包含“str”的唯一值,并返回数字 3。

有什么建议吗?网络上对这个问题充满了半答案,但我花了一个小时试图找到一个明确的解决方案,所以我认为 StackOverflow 可能会从这个问题中受益。

I want to count all the unique values in an Excel column that contain a certain string. For instance, say I have this in my column:

strA
strB
strA
strC
diffA
diffB
strC

If I give this theoretical function that I'm writing a value of "str," it will count the unique values containing "str", and give me back the number 3.

Any suggestions? The web is full of half-answers to this problem, but I've spent an hour trying to find a clear solution, so I thought StackOverflow might benefit from the question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄荷港 2024-09-25 06:20:18
Option Explicit

Sub Test()
    GetCount 1, "str"
End Sub

Sub GetCount(colNum As Integer, str As String)
    Dim row As Integer
    Dim dict
    Set dict = CreateObject("Scripting.Dictionary")

    row = 1

    Dim key As String
    While Cells(row, colNum) <> ""
        key = Cells(row, colNum)
        If InStr(key, str) And Not dict.Exists(key) Then
            dict.Add key, key
        End If

        row = row + 1
    Wend

    MsgBox ("the count is " & dict.Count)

End Sub

当使用输入样本运行时,结果为 3。循环的终止条件是在列中找到第一个空单元格。如果逻辑不是你想要的,你可以调整它。

您需要在 VBA 部分添加对 Microsoft Scripting Runtime 的引用才能编译和运行该宏。

Option Explicit

Sub Test()
    GetCount 1, "str"
End Sub

Sub GetCount(colNum As Integer, str As String)
    Dim row As Integer
    Dim dict
    Set dict = CreateObject("Scripting.Dictionary")

    row = 1

    Dim key As String
    While Cells(row, colNum) <> ""
        key = Cells(row, colNum)
        If InStr(key, str) And Not dict.Exists(key) Then
            dict.Add key, key
        End If

        row = row + 1
    Wend

    MsgBox ("the count is " & dict.Count)

End Sub

When run with your input sample, this gives 3. The terminating condition for the loop is the first empty cell found in a column. You can adjust that if the logic isn't what you want.

You need to add a reference to Microsoft Scripting Runtime in the VBA section for this macro to compile and run.

离不开的别离 2024-09-25 06:20:18
=SUM(1/COUNTIF(A1:A7,A1:A7)*(LEFT(A1:A7,3)="str"))

使用 Control+Shift+Enter 输入,而不仅仅是 Enter,因为它是数组公式。通常,要计算列表中唯一项目的数量,您可以输入

=SUM(1/COUNTIF(A1:A7,A1:A7))

I 将 COUNTIF 部分乘以另一个条件(以 str 开头),以便那些不以字符串开头的项目被视为零。如果您不熟悉数组公式,请参阅

http://www.dailydoseofexcel.com/archives/2004/04/05/anatomy-of-an-array-formula/

=SUM(1/COUNTIF(A1:A7,A1:A7)*(LEFT(A1:A7,3)="str"))

Enter with Control+Shift+Enter, not just Enter, because it's an array formula. Typically to count the number of unique items in the list, you would array enter

=SUM(1/COUNTIF(A1:A7,A1:A7))

I multiplied the COUNTIF portion by another criterion (begins with str) so that those that don't begin with the string are treated as zero. If you're new to array formulas, see

http://www.dailydoseofexcel.com/archives/2004/04/05/anatomy-of-an-array-formula/

昵称有卵用 2024-09-25 06:20:18

我太慢了,但我得到了不同的解决方案:

将要搜索的字符串放入 B1 中,然后输入数组公式:

=SUM(NOT(ISERROR(SEARCH(B1;A1:A7)))*(1/COUNTIF(A1:A7,A1:A7))

I have been too slow, but I got a different solution:

Put the string to search in B1 then Enter as an Array Formula:

=SUM(NOT(ISERROR(SEARCH(B1;A1:A7)))*(1/COUNTIF(A1:A7,A1:A7))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文