2009-05-28 27 views

回答

2

下面是一個示例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 
+0

這正是我想要的。謝謝! – 2009-05-29 14:56:57

相關問題