0
我有一個包含單列字符串(大約400行)的Excel文檔。我也有一個Word文檔,可能包含或不包含Excel文檔中的字符串。如何讓Excel在該單個列中的每行中執行查找,並檢索給定字符串出現的次數?我只有Office 2003.Excel 2003是否在Word文檔中查找並返回出現次數
謝謝!
我有一個包含單列字符串(大約400行)的Excel文檔。我也有一個Word文檔,可能包含或不包含Excel文檔中的字符串。如何讓Excel在該單個列中的每行中執行查找,並檢索給定字符串出現的次數?我只有Office 2003.Excel 2003是否在Word文檔中查找並返回出現次數
謝謝!
下面是一個示例Excel宏,它計算匹配的數量並將其寫在尋找的字符串旁邊。我在Office 2007中嘗試過,但它也應該可以在2003年使用。該宏使用正則表達式,因此您需要將對「Microsoft VBScript正則表達式」庫的引用添加到VBA項目(Visual Basic編輯器 - >工具 - >參考)中。
Sub GetMatchCount()
Dim Text, i, re
' Replace with your Word document name
Const WordFileName = "C:\Test.doc"
With CreateObject("Word.Application")
.Documents.Open (WordFileName)
Text = .ActiveDocument.Range.Text
.Quit
End With
Set re = New RegExp
re.Global = True
With ActiveSheet.UsedRange
For i = 1 To .Rows.Count
re.Pattern = .Cells(i, 1).Value
.Cells(i, 2).Value = re.Execute(Text).Count
Next
End With
End Sub
這正是我想要的。謝謝! – 2009-05-29 14:56:57