【发布时间】:2019-03-28 06:42:02
【问题描述】:
我正在编写一个代码,它将获取 VBA 中文本的开始和结束索引。 此条件应用于单元格时应相应更改
这是一个计算单词长度的代码,但是卡在了索引部分, 我的 StartIndex 函数的输出总是 0。我想再做一个 EndIndex 函数
Public Function CountWords(ByVal strText As String) As Long
Application.Volatile
CountWords = UBound(Split(strText, " ")) + 1
End Function
Length = 1
StartIndex = 0
Public Function StartIndex(ByVal strText As String) As Long
Application.Volatile
StartIndex = Length + StartIndex
Length = UBound(Split(strText, " ")) + 1
End Function
考虑这个例子,我有 col 1 并且想要生成 startIndex 和 endIndex
Col1 startIndex endIndex
VBA Index Printer Friendly version 1 5
Adobe Acrobat version 6 8
A UDF can remain in a code module 9 15
【问题讨论】:
-
你不能在方法之外有像
Length = 1这样的语句。 -
第二行要 1 和 3 吗?同样的,你想要 1 和 8 在第 3 行吗?
-
不,看 col1 , startIndex, endIndex ...它显示了每个单词的索引位置,应该根据前一个单元格不断更新
-
那么最后一个应该是16而不是15?
-
是的,16,但你知道我该怎么做吗?我真的坚持下去了......
标签: excel vba user-defined-functions