I am trying to find a number in a string given via textbox in excel vba and display an error saying it cant continue and after that it must stop from loading the codes below.
fname is Full Name given by the user
If InStr(fname, "0") Then
MsgBox ("Number Found In Your Name. Please Correct That!!")
Dim i(0 To 9) As String
i(0) = "0"
i(1) = "1"
i(2) = "2"
i(3) = "3"
i(4) = "4"
i(5) = "5"
i(6) = "6"
i(7) = "7"
i(8) = "8"
i(9) = "9"
If InStr(fname, "0") Then
MsgBox ("Number Found In Your Name. Please Correct That!!")
End If
It is supposed to search in the text given by the user and display an error if it contains a number(list of numbers)
InStr function is not boolean, it returns integer value. It will give you nth number of the string(fname), if your searching expression(for example "0") is inside string(fname). Otherwise, it returns zero. So, you can try your code like this:
For i=0 to 9
If InStr(fname, i)>0 Then
MsgBox ("Number Found In Your Name. Please Correct That!!")
End If
Next i
Maybe a simple answer, but Cstr() changes integers into strings.
For i = 0 To 9
If InStr(fname, CStr(i)) Then
MsgBox ("Number Found In Your Name. Please Correct That!!")
Exit For
End If
Next
无需循环,只需一行即可完成:
If Fname like "*[0-9]*" Then MsgBox ("Number Found In Your Name. Please Correct That!!")
Regex is the best way, but a loop can also work:
Sub dural()
Dim s As String, i As Long
s = "james"
For i = 0 To 9
If InStr(1, s, CStr(i)) > 0 Then
MsgBox ("Number Found In Your Name. Please Correct That!!")
Exit Sub
End If
Next i
End Sub
You could try implement `EVALUATE'
If Evaluate("COUNT(FIND(ROW(1:10)-1,""" & fname & """))") > 0 then
Msgbox "Number Found In Your Name. Please Correct That!!"
End if
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.