我在下面的代碼位置非常接近我期望的位置。工作原理是在excel電子表格中按下「List Word Issue」按鈕,然後逐個單元格掃描所有文本,並逐行掃描A列中的單個工作表,其中包含單詞列表。如果存在匹配(列1中每個單獨單元格之間存在什麼內容),則它將匹配到列b中的相鄰行的單詞放入。Excel VBA字符匹配計數修復
這裏(http://mintywhite.com/more/software-more/microsoft-excel-analyze-free-text-surveys-feedback-complaints-part-2)是我找到代碼的文章的鏈接和下載整個.xls電子表格的鏈接(http://mintywhite.com/wp-content/uploads/2011/02/wordcount2.xls)。
我在找的是一個簡單的更改,所以不會出現「匹配」,除非該單詞在第一個工作表的A列中的每個單元格/行中至少出現5次。
Sub WordCount()
Dim vArray, WordIssue, ElementCounter As Variant
Dim lngLoop, lngLastRow As Long
Dim rngCell, rngStoplist As Range
ElementCounter = 2 'setting a default value for the counter
Worksheets(1).Activate
For Each rngCell In Worksheets("Word").Range("A3", Cells(Rows.Count, "A").End(xlUp))
vArray = Split(rngCell.Value, " ") 'spliting the value when there is a space
vrWordIssue = ""
ElementCounter = ElementCounter + 1 'increases the counter every loop
For lngLoop = LBound(vArray) To UBound(vArray)
If Application.WorksheetFunction.CountIf(Sheets("Issue").Range("A2:A" & Sheets("Issue").UsedRange.Rows.Count), vArray(lngLoop)) > 0 Then 'this is to test if the word exist in the Issue Sheet.
If vrWordIssue = "" Then
vrWordIssue = vArray(lngLoop) 'assigning the word
Else
If InStr(1, vrWordIssue, vArray(lngLoop)) = 0 Then 'a binary of comparison
vrWordIssue = vrWordIssue & ", " & vArray(lngLoop) 'this will concatinate words issue that exist in Issue Sheet
End If
End If
End If
Next lngLoop
Worksheets("Word").Range("B" & ElementCounter).Value = vrWordIssue 'entering the final word issue list into cell.
Next rngCell
End Sub
你到目前爲止試過了什麼?另外,請注意,您沒有正確確定變量的大小。 'lngLoop'和'rngCell'類型是Variant。 – 2013-03-04 20:18:24