Validating Excel VBA for repeated rows

I have user input that I want to check for correctness. The user must enter 1 or more character sets separated by commas.

So this is a valid input

  • COM1
  • COM1, COM2,1234

they are wrong

  • COM - only 3 characters
  • COM1,123 - one set - only 3 characters.
  • COM1.1234, abcd - non-comma separator of points

I looked for a regex pattern for it and found a possible pattern that checked a repeating instance of any three characters, and I changed like that

/ ^ (. {4}). * \ 1 $ /

but it does not find a match.

I can control the last comma, which may or may not be there before moving on to the test so that it always is.

It is advisable that I test letters (in any case) and only numbers, but I can live with any characters.

, VBA, , , , , .

+4
2

, , :

^([A-Z|a-z|0-9]{4},)*[A-Z|a-z|0-9]{4}$

, , , , .

: https://regex101.com/r/Hdv65h/1

+3

"^[\w]{4}(,[\w]{4})*$" 

.

, , , . , A1 A5 :

Sub findPattern()
  Dim regEx As New RegExp
  regEx.Global = True
  regEx.IgnoreCase = True
  regEx.Pattern = "^[\w]{4}(,[\w]{4})*$"
  Dim i As Integer
  Dim val As String
  For i = 1 To 5:
    val = Trim(Cells(i, 1).Value)
    Set mat = regEx.Execute(val)
    If mat.Count = 0 Then
      MsgBox ("No match found for " & val)
    Else
      MsgBox ("Match found for " & val)
    End If
 Next
End Sub
+2

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


All Articles