Excel Vba-like function

I am so confused about this issue:

VBA syntax has this function:

If Left("abcdef",3)="abc" then 
    Msgbox "True"

This function is too simple, but is there a way, for example,

To have something like this

If left("abcdef",3) is in ["A".."Z"] or if left("1265avd0",2) is in [1..9]

What is the average check if left ("abcdef",3), which is equal "abc", is in this Interval ["A" .. "Z"] or the check if the left("123avd0",2)is numerical and is in the interval [1..9]

I hope you understand what I want to do

Can anyone ignite me on this?

+4
source share
2 answers

You can use regular expressions ("RegEx") for this.

, (?:...), , ^ : [a-zA-Z]{3} | \d{2}.

, MsgBox True, False.

Sub test()

    Dim RegEx As New RegExp, testString As String

    testString = "53bcko390872"

    With RegEx
        .Pattern = "^(?:[a-zA-Z]{3}|\d{2})"
        MsgBox .test(testString)    'will prompt True/False
    End With

End Sub

MsgBox - ,

If .test(testString) [= True] Then...

().

, Microsoft VBScript Regular Expressions x.x

+1

Like .

Option Explicit
Option Compare Text 'for case insensitive matching
Sub dural()
    Const S1 As String = "abcdef"
    Const S2 As String = "1265avd0"

Debug.Print Left(S1, 3) Like "[A-Z][A-Z][A-Z]"
Debug.Print Left(S2, 2) Like "[1-9][1-9]"
End Sub

TRUE .

@chrisneilsen, :

S1 Like "[A-Z][A-Z][A-Z]*"
S2 Like "[1-9][1-9]*"    

- :

If Left(myString, 3) Like "[A-Z][A-Z][A-Z]" or _
  Left(myString, 2) Like "[1-9][1-9]" then
   'do something
End if

:

If myString Like "[A-Z][A-Z][A-Z]*" or _
    myString Like "[1-9][1-9]*" then
   'do something
End if

.

Option Compare, - "[A-Za-z]"

+3

Source: https://habr.com/ru/post/1695372/


All Articles