简体   繁体   English

Excel中的SELECT(DISTINCT COUNT)

[英]SELECT (DISTINCT COUNT) in Excel

I have done this without much effort on T-SQL, but I want to implement the same on Excel VBA. 我在T-SQL上做了很多努力,但我想在Excel VBA上实现相同的功能。 How to achieve this? 怎么做到这一点? I have a field on my sheet called "Stops" which pertains to the Stops that a truck makes while delivering its order. 我的表单上有一个名为“Stops”的字段,该字段与卡车在下达订单时所生成的停靠点有关。 I want to count the total number of stops. 我想计算停站总数。 1 stop can have multiple orders to be delivered. 1个停止可以有多个订单交付。 The data on the stops column is something like: 停止栏上的数据类似于:

Order#  Stops
1527305 1
1527305 1
1529418 2
1529418 2
1527299 3
1527299 3
1528894 5
1528894 5
1529529 6
1529529 6
1529518 7
1529518 7
1527522 8
1527522 8

So, the final count should be just 7 unique stops. 因此,最终计数应该只有7个独特的停止点。 The current code looks at the last row and takes it as the total stops (which is wrong). 当前代码查看最后一行并将其作为总停止(这是错误的)。 The code that I have right now is as follows: 我现在的代码如下:

    ActiveCell.Offset(0, 7).Select  ' H = stop number
    Selection.Value = curStop 'sets stop number
        If Selection.Value = 0 Then
            ActiveCell.Offset(-1, 0).Select
            curStop = ActiveCell.Value
            ActiveCell.Offset(1, 0).Select
            Selection.Value = curStop
        End If

I have done something on T-SQL with one line of code, but dont know how to do this on Excel. 我在T-SQL上用一行代码做了一些事情,但是不知道如何在Excel上做这件事。 Any help will be appreciated. 任何帮助将不胜感激。 Thanks!! 谢谢!!

You can also try 你也可以试试

=SUM(IF($B$2:$B$15,1/COUNTIF($B$2:$B$15,$B$2:$B$15)))

as an array formula solution, assuming your data is in B2:B15. 作为数组公式解决方案,假设您的数据在B2:B15中。 Array formulae are, of course, entered with a Ctrl + Shift + Enter, rather than an enter, so maybe your solution is slightly better :) 当然,使用Ctrl + Shift + Enter输入数组公式,而不是输入,所以也许你的解决方案稍微好一些:)

Also shouldn't your question say the answer is 7 stops, and not 5 ??? 也不应该你的问题说答案是7站,而不是5 ???

You already have a formula-based solution, and that may be what you wish to use in this case. 您已经有了基于公式的解决方案,这可能是您希望在这种情况下使用的解决方案。
If you are curious about a way to solve the problem using VBA, read on. 如果您对使用VBA解决问题的方法感到好奇,请继续阅读。

For the problem of identifying unique values a dictionary can be an appropriate data-structure. 对于识别唯一值的问题,字典可以是适当的数据结构。
In particular, you may check for a key's existence before adding a key. 特别是,您可以在添加密钥之前检查密钥的存在。
Tested example follows: 测试的例子如下:

(Please note: you must enable (check the box) Tools > References > "Microsoft Scripting Runtime".) (请注意:您必须启用(选中复选框)工具>参考>“Microsoft Scripting Runtime”。)

Option Explicit

Function uniq_dict(ByRef row As Long, ByRef col As Long)
    Dim dict As New Scripting.Dictionary
    Dim ws As Worksheet
    Set ws = ActiveWorkbook.Sheets("Sheet1")

    For row = 1 To ws.Cells(Rows.Count, col).End(xlUp).row
        If Not dict.Exists(ws.Cells(row, col).Value) Then
            dict.Add ws.Cells(row, col).Value, Null '# can insert something here 
        End If
    Next row

    Set uniq_dict = dict
End Function

Sub call_uniq_dict()
    Dim i As Integer
    Dim k() As Variant
    Dim dict As New Scripting.Dictionary
    Set dict = uniq_dict(1, 1)

    Debug.Print "total items in dict:", dict.Count
    k = dict.Keys
    For i = 0 To dict.Count - 1
        Debug.Print "  dict key:", k(i)
    Next
End Sub

Result: 结果:

total items in dict:         5 
  dict key:    1 
  dict key:    2 
  dict key:    3 
  dict key:    7 
  dict key:    8

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM