提取Excel中两个字符之间的字符串
你的问题是一点点在缺少细节,但如何:
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我不明白。 –
完美!非常感谢:) –
嗨,你知道一种方法来确保最后3个字符是数字吗? –