查找名称为" x"的列并计算该列中的单词出现次数

时间:2015-06-21 20:13:56

标签: excel vba excel-vba

我有一个带标题的数据工作表。我正在尝试在VBA中找到带有标题的列#34;键入"然后在该列中计算字符串" x"的次数。出现,即计算次数"添加"出现在带有标题"列"。

的列中

我知道您可以创建一个脚本字典来计算每个单词出现的次数,我在搜索标题时遇到问题以找到列"键入"。

到目前为止,我的代码会查看工作表中的每个单元格,但我只想将其限制为列#34;键入":

Dim shtSheet1 As String
Dim dict As Object
Dim mycell As Range

shtSheet1 = "Test"   

Set dict = CreateObject("Scripting.Dictionary")

dict.Add "Add", 0
dict.Add "Delete", 0
dict.Add "Update", 0

For Each mycell In ActiveWorkbook.Worksheets(shtSheet1).UsedRange
If dict.Exists(ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value) Then
        dict(ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value) = dict(ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value) + 1
End If
Next

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

我会使用此代码块来遍历列标题。此外,我将使用工作表函数COUNTIF,因此您只需遍历列标题而不是范围内的每个单元格。

'use strict'

angular.module 'myApp'
.directive 'nextButton', [() ->
  return {
    restrict: 'A'
    replace: true  
    transclude: false
    scope: {
      saveLocal: '&saveDataLocal'
      nextTab: '='
      formName : '='
    }
    link: (scope, elem, attrs) -> 
      console.log scope.saveLocal
      elem.bind 'click', ()->
        angular.element('#'+scope.formName).submit()
        if not angular.element('#'+scope.formName).hasClass 'ng-invalid'
          angular.element('#'+scope.nextTab).trigger('click')
  }
]

答案 1 :(得分:0)

如果我理解正确,那么你可以使用它:

Sub test()
    Dim Dict As Object: Set Dict = CreateObject("Scripting.Dictionary")
    Dim shtSheet1 As Worksheet: Set shtSheet1 = Sheets("Test")
    Dim mycell As Range, n&, z&
    Dim Fx As Object, Key As Variant
    Set Fx = WorksheetFunction
    Dict.CompareMode = vbTextCompare
    With shtSheet1
        n = .Rows(1).Find("Type").Column
        z = .Cells(.Rows.Count, n).End(xlUp).Row
        For Each mycell In .Range(.Cells(2, n), Cells(z, n))
            If Not Dict.Exists(Fx.Trim(mycell)) Then Dict.Add Fx.Trim(mycell), 0
        Next
        For Each mycell In .Range(.Cells(2, n), Cells(z, n))
            If Dict.Exists(Fx.Trim(mycell)) Then
                 Dict(Fx.Trim(mycell)) = CLng(Dict(Fx.Trim(mycell))) + 1
            End If
        Next
    End With
    For Each Key In Dict
        Debug.Print Key, Dict(Key)
    Next Key
End Sub

输出数据示例如下:

enter image description here

<强>更新
使用worksheetfunction.countif和字典

的变体
Sub test2()
    Dim Dict As Object: Set Dict = CreateObject("Scripting.Dictionary")
    Dim shtSheet1 As Worksheet: Set shtSheet1 = Sheets("Test")
    Dim mycell As Range, n&, Data As Range
    Dim Fx As Object, Key As Variant
    Set Fx = WorksheetFunction
    Dict.CompareMode = vbTextCompare
    With shtSheet1
        n = .Rows(1).Find("Type").Column
        Set Data = .Range(.Cells(2, n), Cells(.Cells(.Rows.Count, n).End(xlUp).Row, n))
        For Each mycell In Data
            If Not Dict.Exists(Fx.Trim(mycell)) Then Dict.Add Fx.Trim(mycell), Fx.CountIf(Data, "*" & Fx.Trim(mycell) & "*")
        Next
    End With
    For Each Key In Dict
        Debug.Print Key, Dict(Key)
    Next Key
End Sub