回答
你的问题是一点点在缺少细节,但如何:
Sub test()
Debug.Print containsXnumbers("KT150", 3)
End Sub
Function containsXnumbers(sInput As String, xNumbers As Long) As Boolean
Dim x As Long
Dim numCount As Long
For x = 1 To Len(sInput)
If IsNumeric(Mid(sInput, x, 1)) Then numCount = numCount + 1
Next x
If numCount = xNumbers Then containsXnumbers = True
End Function
这应有助于:
Function onlyDigits(s As String) As String
' Variables needed (remember to use "option explicit"). '
Dim retval As String ' This is the return string. '
Dim i As Integer ' Counter for character position. '
' Initialise return string to empty '
retval = ""
' For every character in input string, copy digits to '
' return string. '
For i = 1 To Len(s)
If Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" Then
retval = retval + Mid(s, i, 1)
End If
Next
' Then return the return string. '
onlyDigits = retval
End Function
例子:
Dim myStr as String
myStr = onlyDigits ("3d1fgd4g1dg5d9gdg")
MsgBox (myStr)
将返回(在消息框中):
314159
*代码是this SO answer
我怎么会增加对有多少个数字计数器?我试过.Count功能,但似乎并没有开箱即用。 –
您可以保留一个计数器变量并在if条件中的For循环中增加它。 – Dev
或者只是把'Len(myStr)'作为输出。 –
你不需要VBA得到一个字符串的位数,但这里是一个方法来计算他们:
Public Function KountNumbers(r As Range) As Long
Dim i As Long, t As String
t = r.Text
For i = 1 To Len(t)
If Mid(t, i, 1) Like "[0-9]" Then KountNumbers = KountNumbers + 1
Next i
End Function
例如:
没有VBA试试这个:
=SUMPRODUCT(LEN(A1)-LEN(SUBSTITUTE(A1,{0,1,2,3,4,5,6,7,8,9},"")))
获得数字的数量。
我需要r是字符串,并尽可能返回字符串 –
@JV我不明白。 –
- 1. 提取Haskell中两个子字符串之间的字符串
- 2. 如何在Excel中的两个不同字符串之间提取字符串
- 3. 提取两个字符之间的子字符串中的C#
- 4. 提取两个字符串之间的字符串
- 5. 从两个字符之间的字符串中提取信息
- 6. 提取字符串中两个字符之间的子字符串PHP
- 7. 在两个字符串之间提取xml作为字符串
- 8. 如何提取两个“/”字符之间的字符串
- 9. 提取两个字符之间的字符串
- 10. 提取两个字符之间的字符串 - Matlab(regexp)
- 11. 提取两个字符之间的字符串
- 12. 提取两个字符串之间不同的字符
- 13. 两个字符串之间不同的行提取字符
- 14. 如何提取两个管道字符之间的字符串?
- 15. 两个字符之间的字符串提取
- 16. 如何提取两个字符之间的字符串?
- 17. 提取两个字符之间的所有字符串powershell
- 18. 在python中提取2个字符串之间的字符串?
- 19. 提取R中的其他两个字符串之间的字符串
- 20. 在网站中提取两个字符串的子串之间
- 21. 提取字符串,字符串X和字符串之间ÿ
- 22. 使用sed获取两个字符串之间的字符串
- 23. 获取两个字符串之间的字符串
- 24. 获取两个字符串之间的字符串?
- 25. 如何在NSMutablestring中的iOS中的两个字符之间提取字符串?
- 26. 提取符号之间的字符串
- 27. 获取unix中两个特定字符之间的字符串
- 28. 如何获取Ruby中两个字符之间的字符串?
- 29. C#获取两个字符之间字符串中的字符串
- 30. 如何提取两个不同字符串之间的数字
完美!非常感谢:) –
嗨,你知道一种方法来确保最后3个字符是数字吗? –