
我需要計算a中的總單字數微軟Excel文件。通常,在 MS Word 或 PowerPoint 中,它顯示在狀態列或屬性視窗中。但在 Excel 中,它並沒有在任何地方給出。
有解決辦法嗎?
答案1
您可以為此建立一個巨集:
按 ALT + F11 並輸入以下代碼。
然後選擇整個工作表運行巨集進行字數統計。您也可以“只需選擇一個部分”和“字數統計”即可選擇該部分。
Sub CountWords()
Dim MyRange As Range
Dim CellCount As Long
Dim TotalWords As Long
Dim NumWords As Integer
Dim Raw As String
Set MyRange = ActiveSheet.Range(ActiveWindow.Selection.Address)
TotalWords = 0
For CellCount = 1 To MyRange.Cells.Count
If Not MyRange.Cells(CellCount).HasFormula Then
Raw = MyRange.Cells(CellCount).Value
Raw = Trim(Raw)
If Len(Raw) > 0 Then
NumWords = 1
Else
NumWords = 0
End If
While InStr(Raw, " ") > 0
Raw = Mid(Raw, InStr(Raw, " "))
Raw = Trim(Raw)
NumWords = NumWords + 1
Wend
TotalWords = TotalWords + NumWords
End If
Next CellCount
MsgBox "There are " & TotalWords & " words in the selection."
End Sub