vba - 字符串处理参数
问题描述
Public Function GetAllUndershoreVaues() As Collection
On Error GoTo ErrorHandler
Dim colAux1 As Collection
Dim sentence As Range
Dim w As Words
For Each sentence In ActiveDocument.StoryRanges
For Each w In sentence.Words
If (InStr(w, "-") > 0) Then
If Right(w, "-") Then
colAux1.Add w, UCase$(w)
Else
Set colAux1 = Nothing
End If
Else
Set colAux1 = Nothing
End If
Next
Next
Set GetAllUndershoreVaues = colAux1
Set colAux1 = Nothing
Set sentence = Nothing
Set w = Nothing
End Function
得到编译错误:
参数不是可选的
解决方案
循环遍历每个故事范围中的每个单词是非常低效的。尝试以下方式:
Public Function GetAllUndershoreVaues() As Collection
Dim colAux1 As Collection, Rng As Range, StrItem As String
Set colAux1 = New Collection
For Each Rng In ActiveDocument.StoryRanges
With Rng
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "-*>"
.Replacement.Text = ""
.Forward = True
.Format = False
.Wrap = wdFindStop
.MatchWildcards = True
End With
Do While .Find.Execute = True
StrItem = Split(.Text, "-")(1)
colAux1.Add StrItem, UCase$(StrItem)
.Collapse (wdCollapseEnd)
Loop
End With
Next
Set GetAllUndershoreVaues = colAux1: Set colAux1 = Nothing
End Function
推荐阅读
- r - How to replace NAs in multiple columns with dplyr
- t4 - 当我升级到 .Net 4.8 时,T4 模板程序集加载停止运行
- java - 无法删除父实体,因为双向映射案例中的子引用
- python - Pyinstaller 在 onedir 模式下显示控制台
- javascript - 快速会话中间件中秘密的目的是什么?
- python - 如何在 PyCharm 中找到保存的文件
- typescript - 如何在 Typescript 中创建仅包含非空/未定义属性的对象?
- angular - 错误类型错误:google.maps.Travelmode 未定义,即使已定义
- json - From table to JSON with Power BI / Power Query
- flutter - Flutter 中的 IBM Watson Assistant:如何显示选项?