我打算假定所有的句子都是單行,並且在單詞之間包含一個空格。將名爲「輸出」的工作表添加到工作簿中。在單元格A1中鍵入一個標題(例如「Word」),並在單元格B2中鍵入一個標題(例如「Count」)。下面的句子將輸出A列中的單詞和B列中單詞的計數,然後進行排序,最常用的是排在最前面。根據你有多少數據,這應該需要一兩秒鐘才能運行。
注意:您需要添加對「Microsoft Scripting Runtime」庫的引用。
Sub Example()
Dim X As Variant, S As Variant, key As Variant
Dim str As String
Dim oDict As Scripting.Dictionary
Dim i As Double, j As Double, k As Double
Dim Anchor As Range
Set oDict = New Scripting.Dictionary
With ThisWorkbook
'Clear past output
With .Sheets("Output")
.Range("a2:" & .Cells(.Rows.Count, .Columns.Count).Address).ClearContents
End With
'Fill array with text to search
With .Sheets("Raw")
X = .Range("a2:a" & .Range("a" & .Rows.Count).End(xlUp).Row).Value2
End With
End With
For i = LBound(X,1) To UBound(X,1)
S = Split(X(i,1), " ")
For j = LBound(S, 1) To UBound(S, 1)
If oDict.Exists(S(j)) Then
oDict.Item(S(j)) = oDict.Item(S(j)) + 1
Else
oDict.Add S(j), 1
End If
Next j
Next i
'Output results to sheet "Output"
With ThisWorkbook.Sheets("Output")
For Each key In oDict.Keys
Set Anchor = .Range("a" & Rows.Count).End(xlUp).Offset(1, 0)
Anchor = key
Anchor.Offset(0, 1) = oDict.Item(key)
Next key
.Range("a1:" & .Range("a" & .Rows.Count).End(xlUp).Offset(0, 1).Address).Sort .Range("b:b"), xlDescending
End With
End Sub
編輯:
這是我完全的,純粹的代碼。請注意,工作簿和工作表參考不會根據您的目的進行更新。要使用RegExp,您需要添加對「Microsoft VBScript Regular Expressions 5.5」庫的引用。我使用「5.5」,但我相信任何會爲此工作。
Sub Example()
Dim X As Variant, S As Variant, S2 As Variant, S3 As Variant, key As Variant
Dim oDict As Scripting.Dictionary
Dim i As Double, j As Double, k As Double
Dim Anchor As Range
Dim oReg As New RegExp
Dim str As String
Dim st As Single
Application.ScreenUpdating = False
st = Timer
Set oDict = New Scripting.Dictionary
With ThisWorkbook
'Clear past output
With .Sheets("Output")
.Range("a2:" & .Cells(.Rows.Count, .Columns.Count).Address).ClearContents
End With
'Fill array with text to search
With .Sheets("Input")
X = .Range("a2:a" & .Range("a" & .Rows.Count).End(xlUp).Row).Value2
End With
End With
With oReg
.Global = True
.IgnoreCase = True
.Pattern = "[^\w\s]"
End With
For i = LBound(X, 1) + 1 To UBound(X, 1)
'Get rid of none letter and white space
str = oReg.Replace(X(i, 1), "")
S = Split(str, " ")
For j = LBound(S, 1) To UBound(S, 1)
If oDict.Exists(LCase(S(j))) Then
oDict.Item(LCase(S(j))) = oDict.Item(LCase(S(j))) + 1
Else
oDict.Add LCase(S(j)), 1
End If
Next j
Next i
'Output results to sheet "Output"
With ThisWorkbook.Sheets("Output")
For Each key In oDict.Keys
Set Anchor = .Range("a" & Rows.Count).End(xlUp).Offset(1, 0)
Anchor = key
Anchor.Offset(0, 1) = oDict.Item(key)
Next key
.Range("a1:" & .Range("a" & .Rows.Count).End(xlUp).Offset(0, 1).Address).Sort .Range("b:b"), xlDescending
End With
Debug.Print Timer - st
Application.ScreenUpdating = True
End Sub
如果您的代碼正常工作,但您希望它得到改進,那麼您應該考慮刪除此處的問題並將其發佈到http://codereview.stackexchange.com/。 –
我甚至不知道存在。非常感謝你John! –
知道該網站存在是有用的,但現在你已經至少有一個答案,你應該把它留在這裏,因爲交叉發佈是不受歡迎的。 –