Check for a combination of characters

aurelius89

Board Regular
Joined
Mar 15, 2017
Messages
69
Hi,

I need some vba to check if a cell contains the combination of NUMBER LETTER NUMBER or starts with a Letter

However, not sure where to even begin with this...

It will be part of a much much larger code, but for a simple example lets say cell A1 is the cell to check and the msgbox says "yes" or "no" if it matches

Code:
If A1 = ????? Then
Msgbox "yes"
Else
Msgbox "no"
End If

Combinations that would trigger the match:

123abc1
1a3333
45bbb111
k23
PP1
MMMM8

Combinations that would not:

12a
1111111111k
989
7

Anyone point me in the right direction?

Thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Will all your entries only be numbers and letters, or might you also have spaces, punctuation, or other special characters (I am only concerned with what is showing up in the first three spaces)?
 
Upvote 0
Also, I see a discrepancy with your examples:
Combinations that would trigger the match:

123abc1
1a3333
45bbb111
k23
PP1
MMMM8

Neither of the two highlighted in red seem to meet your conditions of:
- starts with a letter
- first 3 characters are NUMBER-LETTER-NUMBER
 
Upvote 0
Sorry I should of made it clearer - There could be any number of letters or numbers, but if there is X amount letters, followed by X amount of numbers, followed by X amount letters then that should meet the condition for "yes"

Essentially (And another way of thinking about it I guess) I am trying to verify that the cell contains X amount of numbers, POSSIBLY followed by X amount of letters.
Anything other combination is junk.

There will be no spaces or other characters. Purely letter and number.
 
Last edited:
Upvote 0
OK, I just need to confirm what you are really after. You first said:
NUMBER LETTER NUMBER or starts with a Letter
Then you said:
X amount letters, followed by X amount of numbers, followed by X amount letters
and finally:
X amount of numbers, POSSIBLY followed by X amount of letters.
So, your first seems to indicate, numbers followed by letters followed by numbers.
Your second seems to indicate the opposite, letters followed by number followed by letters
And finally your third seems to indicate just numbers followed by letters

So which of these three combinations is actually correct, as they are all different?
 
Upvote 0
Like i say, X amount of numbers, POSSIBLY followed by X amount of letters is what the cell should contain. Anything else is junk.

X amount letters, followed by X amount of numbers, followed by X amount letters is a common combination that could be in the cell, but it is junk.
X amount of numbers, followed by X amount of letters, followed by X amount of numbers is another combination that is junk.
Just X amount of letters is junk.
 
Upvote 0
OK, but that is NOT what you said:

if there is X amount letters, followed by X amount of numbers, followed by X amount letters then that should meet the condition for "yes"
X amount letters, followed by X amount of numbers, followed by X amount letters is a common combination that could be in the cell, but it is junk.
So, in post 4, you said it should return "yes", but in post 6 you said it is "junk".


And originally, you said:
I need some vba to check if a cell contains the combination of NUMBER LETTER NUMBER or starts with a Letter
but in post #6 you said:
Just X amount of letters is junk.
So I am not clear if an entry starts with letters if it is junk or not.


And in post #6 , you said:
X amount of numbers, followed by X amount of letters, followed by X amount of numbers is another combination that is junk.
yet in your original post you said:
I need some vba to check if a cell contains the combination of NUMBER LETTER NUMBER
and gave "matching" example like:
123abc1
1a3333
45bbb111


So, you can see my confusion. It doesn't seem to make much sense, as you seem to keep contradicting yourself with each post.
Or is "yes" equivalent to "junk"?
 
Upvote 0
Just guessing ...

A​
B​
C​
1​
Input
Output
2​
123abc1yesB2: =aurelius(A2)
3​
1a3333yes
4​
45bbb111yes
5​
k23yes
6​
PP1yes
7​
MMMM8yes
8​
12ajunk
9​
1111111111kjunk
10​
989junk
11​
7junk

Code:
Function aurelius(sInp As String) As String
  Dim i             As Long
  Dim sPatt         As String

  For i = 1 To Len(sInp)
    Select Case Mid(sInp, i, 1)
      Case 0 To 9
        If Right(sPatt, 1) <> "n" Then sPatt = sPatt & "n"
      Case "A" To "Z", "a" To "z"
        If Right(sPatt, 1) <> "l" Then sPatt = sPatt & "l"
      Case Else
        aurelius = "junk"
        Exit Function
    End Select
  Next i

  If sPatt = "ln" Or sPatt = "nln" Then aurelius = "yes" Else aurelius = "junk"
End Function
 
Upvote 0
And if that works, so does this:

Code:
Function aurelius(sInp As String) As String
  With New RegExp
    .Pattern = "^\d*[A-Za-z]+\d+$"
    aurelius = IIf(.Test(sInp), "yes", "junk")
  End With
End Function
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top