计算选择中的所有逗号或选定文本
Count all Comma In Selection or selected text
我想计算所有仅在选定文本中出现的逗号“,”,之后我将使用 Count as Integer
to 运行 the loop
我的问题是如何计算 ,
如下图所示:
我不知道如何使用 split
和 ubound
。以下代码有什么问题?
Sub CountComma()
Dim x As String, count As Integer, myRange As Range
Set myRange = ActiveDocument.Range(Selection.Range.Start, Selection.Range.End)
x = Split(myRange, ",")
count = UBound(x)
Debug.Print count
End Sub
一个简单的分割就可以了。
x = Split("XXX,XXX,XXX,XXX,XX,XX", ",")
Count = UBound(x)
Debug.Print Count
B/c 数组从零开始,您可以按原样使用 Ubound
数字。
编辑:
使用 range
.
x = Split(Range("A1").Value, ",")
分解代码。
Split("A string value","Delimiter to split the string by")
如果你想要一行代码,
x = UBound(Split(myRange, ","))
感谢 KyloRen 和 Cindy Meister,现在我可以使用 split
和 Ubound
在 selection.text
中计算 ,
。
以下是工作代码:
Sub Count_Words()
Dim WrdArray() As String, myRange As String
myRange = ActiveDocument.Range(Selection.Range.Start, Selection.Range.End)
WrdArray() = Split(myRange, ", ")
MsgBox ("Total , in the string : " & UBound(WrdArray()))
End Sub
您的代码在 x
类型的 x
变量的初始声明语句中是错误的,因为在后续语句中
with x = Split(myRange, ",")
你想要 x
保存 Split()
函数的 return 值,它是一个数组(参见 here),因此是 Variant
类型
所以你必须使用
Dim x As Variant
但是你可以简化你的代码如下
Option Explicit
Sub CountComma()
Dim count As Integer
count = UBound(Split(Selection, ","))
Debug.Print count
End Sub
因为:
您不需要任何 Range
类型的变量来存储 Selection
对象,因为 Selection
已经是选定的范围(参见 here )
你也不需要 x
Variant
变量,直接用 UBound()
函数(它期望一个数组作为它的第一个参数)提供 Split()
函数,正如我们在上面看到的,return 只是一个数组!
最后我会给出一个计算范围内逗号的替代方法
Sub CountComma()
Dim countAs Integer
count = Len(Selection) - Len(Replace(Selection, ",", ""))
Debug.Print count
End Sub
我想计算所有仅在选定文本中出现的逗号“,”,之后我将使用 Count as Integer
to 运行 the loop
我的问题是如何计算 ,
如下图所示:
我不知道如何使用 split
和 ubound
。以下代码有什么问题?
Sub CountComma()
Dim x As String, count As Integer, myRange As Range
Set myRange = ActiveDocument.Range(Selection.Range.Start, Selection.Range.End)
x = Split(myRange, ",")
count = UBound(x)
Debug.Print count
End Sub
一个简单的分割就可以了。
x = Split("XXX,XXX,XXX,XXX,XX,XX", ",")
Count = UBound(x)
Debug.Print Count
B/c 数组从零开始,您可以按原样使用 Ubound
数字。
编辑:
使用 range
.
x = Split(Range("A1").Value, ",")
分解代码。
Split("A string value","Delimiter to split the string by")
如果你想要一行代码,
x = UBound(Split(myRange, ","))
感谢 KyloRen 和 Cindy Meister,现在我可以使用 split
和 Ubound
在 selection.text
中计算 ,
。
以下是工作代码:
Sub Count_Words()
Dim WrdArray() As String, myRange As String
myRange = ActiveDocument.Range(Selection.Range.Start, Selection.Range.End)
WrdArray() = Split(myRange, ", ")
MsgBox ("Total , in the string : " & UBound(WrdArray()))
End Sub
您的代码在 x
类型的 x
变量的初始声明语句中是错误的,因为在后续语句中
with x = Split(myRange, ",")
你想要 x
保存 Split()
函数的 return 值,它是一个数组(参见 here),因此是 Variant
类型
所以你必须使用
Dim x As Variant
但是你可以简化你的代码如下
Option Explicit
Sub CountComma()
Dim count As Integer
count = UBound(Split(Selection, ","))
Debug.Print count
End Sub
因为:
您不需要任何
Range
类型的变量来存储Selection
对象,因为Selection
已经是选定的范围(参见 here )你也不需要
x
Variant
变量,直接用UBound()
函数(它期望一个数组作为它的第一个参数)提供Split()
函数,正如我们在上面看到的,return 只是一个数组!
最后我会给出一个计算范围内逗号的替代方法
Sub CountComma()
Dim countAs Integer
count = Len(Selection) - Len(Replace(Selection, ",", ""))
Debug.Print count
End Sub