【发布时间】:2013-03-04 18:59:10
【问题描述】:
我在下面的这段代码非常接近我想要做的事情。它的工作原理是您按下 Excel 电子表格中的“列出单词问题”按钮,它会逐个单元格地扫描 A 列中的所有文本,逐行扫描包含单词列表的单独工作表。如果存在匹配项(在第 1 列中每个单独单元格中的内容之间),则它将匹配的单词放入 b 列中的相邻行中。
此处 (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 类型。