VBA for Groupings of letter and/or numbers to decode

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
774
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Greetings,

I'm trying to decode a alpha numeric code. I have several MACROS and they work fine. I know for example
Excel Formula:
If Data(R, 1) Like "[145678CDJKQXYZF][JPVM][HXZWJURYANM]*"
. That a letter or number within the first braces combined with a letter in the second braces and finally combined with a letter in the third braces will get be the desired text. What I would really like to know: Suppose I have a group of letters/numbers that now rather than a single letter or numbe the macros will look for a pair of letters or numbers. In the example below suppose rather than [HXZWJURYANM] the 3 & 4 characters have to be in pairs preceeded by the braces with single digits, for example (01), (WC), (32) etc.. Again the formula is good below, I just need a little tweak.

Thank you very much indeed.

VBA Code:
Sub Valid_Access()
  Dim R As Long, Data As Variant, Result As Variant
  Data = Range("B1", Cells(Rows.Count, "B").End(xlUp)).Value
  Result = Range("F1").Resize(UBound(Data)).Value
  For R = 1 To UBound(Data)
    If Data(R, 1) Like "[145678CDJKQXYZF][JPVM][HXZWJURYANM]*" Then
      Result(R, 1) = Result(R, 1) & Mid("/Valid Access with Proper ID", 1 - (Result(R, 1) = ""))
    End If
  Next
  Range("F1").Resize(UBound(Result)) = Result
End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Few lines of examples and expected results would help
 
Upvote 0
Here is an example in Column B I have "FJWC207Z1338". Using the macro provided the first and second characters I covered F in the first braces and J is the second braces. For the third braces I need the MACRO to target the third and fourth characters. How can I include a group of two letters like I did for the single letters?

It would be something like
Excel Formula:
If Data(R, 1) Like "[145678CDJKQXYZF][JPVM][(WC)(FT)(05)(X5)]*" Then

Thank you,
 
Upvote 0
I'm not working with somelthing like- Hopefully, it will work.

Thank you,

VBA Code:
Sub Valid_Access()
  Dim RE As Object
  Dim R As Long, Data As Variant, Result As Variant
  Set RE = CreateObject("VBScript.RegExp")
  RE.Pattern = "^[145678CDJKQXYZF][JPVM](WC|FT|05|X5)"
  Data = Range("B1", Cells(Rows.Count, "B").End(xlUp)).Value
  Result = Range("F1").Resize(UBound(Data)).Value
  For R = 1 To UBound(Data)
    If RE.Test(Data(R, 1)) Then
      Result(R, 1) = Result(R, 1) & Mid("/Valid Access with Proper ID", 1 - (Result(R, 1) = ""))
    End If
  Next
  Range("F1").Resize(UBound(Result)) = Result
End Sub
 
Upvote 0
I'm not working with somelthing like- Hopefully, it will work.

Thank you,

VBA Code:
Sub Valid_Access()
  Dim RE As Object
  Dim R As Long, Data As Variant, Result As Variant
  Set RE = CreateObject("VBScript.RegExp")
  RE.Pattern = "^[145678CDJKQXYZF][JPVM](WC|FT|05|X5)"
  Data = Range("B1", Cells(Rows.Count, "B").End(xlUp)).Value
  Result = Range("F1").Resize(UBound(Data)).Value
  For R = 1 To UBound(Data)
    If RE.Test(Data(R, 1)) Then
      Result(R, 1) = Result(R, 1) & Mid("/Valid Access with Proper ID", 1 - (Result(R, 1) = ""))
    End If
  Next
  Range("F1").Resize(UBound(Result)) = Result
End Sub
If I understood what you were trying to do, then your code here is working. However, the code will give error if only one row of data for whatever reason :unsure:
 
Upvote 0
Perhaps something like this:
VBA Code:
tx = Data(R, 1)
    If Left(tx, 2) Like "[145678CDJKQXYZF][JPVM]" Then
        If InStr("|WC|FT|05|X5|", Mid(tx, 3, 2)) Then
            Result(R, 1) = Result(R, 1) & Mid("/Valid Access with Proper ID", 1 - (Result(R, 1) = ""))
        End If
    End If
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,331
Members
452,907
Latest member
Roland Deschain

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