【问题标题】:Count number of occurrences of (unknown) string in excel column计算excel列中(未知)字符串的出现次数
【发布时间】:2014-07-02 20:20:14
【问题描述】:

所以我在 excel 中有一个这样的电子表格:

| some data | PLA      |   
| some data | MADW     |      
| some data | IBFWE    |      
| some data | RWIUBGER |      
| some data | WIUFBW   |      

我想计算每个字符串的出现次数并将其输入到新的电子表格中。有点像键值存储,键作为字符串,值作为出现次数。它需要能够运行多次(每当电子表格有新条目时)。我将如何完成这样的事情?我的电子表格太大(150k 条目),无法手动完成。

我听说过 excel 宏或使用 python 脚本导入工作表然后写入同一个文件,或者以某种方式使用 google 工作表并从 excel 本身调用函数?

【问题讨论】:

  • 您的问题是关于 excel-vba 还是 Google-apps-script ?不能同时...

标签: vba excel google-apps-script


【解决方案1】:

只需在其旁边添加一列,并计算整列的内容:

=COUNTIF(L5:L12,L5)

无论在 L5 中发生什么,都将被计算为在整个范围中找到的次数。在整个列表中执行此操作。

一旦你有了它,你可以将它反弹到另一张纸上,并删除重复的。

【讨论】:

    【解决方案2】:

    您可以设置与工作表的 ADO 连接并在其上运行查询,就像它是数据库一样。

    Dim query_results as variant
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")
    Dim your_file, strSQL as string
    
    your_file = "enter the full path of your excel file this is assuming is .xls"
    
    strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & your_file & ";" & _
    "Extended Properties=""Excel 12.0;HDR=YES"";"
    cn.Open strCon
    strSQL = "Select value, count(whatever_you're_counting) from [ExcelSheetName$] group by value"
    Set rs = cn.Execute(strSQL)
    query_results = rs.GetRows
    rs.Close
    'Code untested
    

    此时您应该能够遍历 query_results 并提取值/计数并将它们放入另一张表中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-06
      • 2015-06-27
      • 2020-12-10
      • 2012-10-08
      • 2019-06-03
      • 1970-01-01
      • 2014-04-24
      相关资源
      最近更新 更多