Separating Drug Data Matrix Information

bobili

New Member
Joined
Feb 20, 2022
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
In the list I want to create, my aim is to read the QR codes of the drugs and quickly separate the 4 information they contain. While I can do this for a single drug name as in the example, I cannot do this with the QR codes created by different drugs and different manufacturers. This is because the serial number and lot number fields are of variable length and the start of the application identifier number and the end of the previous value contain the same value, or the end of the value begins with the application identifier number. I would be glad if you can help with this.

Sub Düğme1_Tıklat()
son = Range("a65536").End(3).Row
For a = 2 To son
b = Mid(Cells(a, 1), 4, 13)
Cells(a, 2) = b
Next a
son = Range("a65536").End(3).Row
For a = 2 To son
b = Mid(Cells(a, 1), 19, 14)
Cells(a, 3) = b
Next a
son = Range("a65536").End(3).Row
For a = 2 To son
b = Mid(Cells(a, 1), 35, 6)
Cells(a, 4) = b
Next a
son = Range("a65536").End(3).Row
For a = 2 To son
b = Mid(Cells(a, 1), 43, 11)
Cells(a, 5) = b
Next a
End Sub
 

Attachments

  • Ekran Alıntısı.JPG
    Ekran Alıntısı.JPG
    92.3 KB · Views: 7

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Welcome to the MrExcel board!

One example is not much to go on so if the following does not meet your needs could we have some more please (with results)?
Also, very hard to copy those details from a picture, so please investigate this:

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.

Test this with a copy of your data.
I have assumed (from your one example) that the first 3 values will be all numerical and the 4th value could include text.

VBA Code:
Sub SplitVals()
  Dim RX As Object
  Dim a As Variant
  Dim i As Long
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "^(010)(\d{13})(21)(\d{1,23})(17)(\d{6})(10)(.{1,22})$"
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    a = .Value
    For i = 1 To UBound(a)
      If RX.Test(a(i, 1)) Then
        a(i, 1) = RX.Replace(a(i, 1), "$2;$4;$6;$8")
      Else
        a(i, 1) = vbNullString
      End If
    Next i
    With .Offset(, 1)
      .Value = a
      .TextToColumns DataType:=xlDelimited, Semicolon:=True, Other:=False, FieldInfo:=Array(Array(1, 2), Array(2, 2), Array(3, 2), Array(4, 2))
      .Resize(, 4).Columns.AutoFit
    End With
  End With
End Sub

Here are my sample data and results

bobili.xlsm
ABCDE
1
2010869952209694721251000013754231724013110A1161007869952209694725100001375423240131A1161007
310236598456325634A
4
50108699522096947212514231724013110A1161008569856544588699522096947251423240131A116100856985654458
Sheet1
 
Upvote 0
Welcome to the MrExcel board!

One example is not much to go on so if the following does not meet your needs could we have some more please (with results)?
Also, very hard to copy those details from a picture, so please investigate this:

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.

Test this with a copy of your data.
I have assumed (from your one example) that the first 3 values will be all numerical and the 4th value could include text.

VBA Code:
Sub SplitVals()
  Dim RX As Object
  Dim a As Variant
  Dim i As Long
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "^(010)(\d{13})(21)(\d{1,23})(17)(\d{6})(10)(.{1,22})$"
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    a = .Value
    For i = 1 To UBound(a)
      If RX.Test(a(i, 1)) Then
        a(i, 1) = RX.Replace(a(i, 1), "$2;$4;$6;$8")
      Else
        a(i, 1) = vbNullString
      End If
    Next i
    With .Offset(, 1)
      .Value = a
      .TextToColumns DataType:=xlDelimited, Semicolon:=True, Other:=False, FieldInfo:=Array(Array(1, 2), Array(2, 2), Array(3, 2), Array(4, 2))
      .Resize(, 4).Columns.AutoFit
    End With
  End With
End Sub

Here are my sample data and results

bobili.xlsm
ABCDE
1
2010869952209694721251000013754231724013110A1161007869952209694725100001375423240131A1161007
310236598456325634A
4
50108699522096947212514231724013110A1161008569856544588699522096947251423240131A116100856985654458
Sheet1
Thank you, I will learn more with your reply
 
Upvote 0
The problem I'm facing here is that some field values have different lengths.
 
Upvote 0
sabit uzunlukta olmayan değer alanları farklı karakterler, metinler veya sayılar içerebilir
non-fixed-length value fields can contain different characters, text or numbers
 
Upvote 0
So, as I said before, if my suggestion does not meet your needs, give us some more sample data with the expected results with XL2BB.
Say 6 or 8 rows with data that shows the sort of variation that can occur.
 
Upvote 0
non-fixed-length value fields can contain different characters, text or numbers
Try this then

VBA Code:
Sub SplitVals_v2()
  Dim RX As Object
  Dim a As Variant
  Dim i As Long
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "^(010)(\d{13})(21)(.{1,23})(17)(\d{6})(10)(.{1,22})$"
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    a = .Value
    For i = 1 To UBound(a)
      If RX.Test(a(i, 1)) Then
        a(i, 1) = RX.Replace(a(i, 1), "$2;$4;$6;$8")
      Else
        a(i, 1) = vbNullString
      End If
    Next i
    With .Offset(, 1)
      .Value = a
      .TextToColumns DataType:=xlDelimited, Semicolon:=True, Other:=False, FieldInfo:=Array(Array(1, 2), Array(2, 2), Array(3, 2), Array(4, 2))
      .Resize(, 4).Columns.AutoFit
    End With
  End With
End Sub

bobili.xlsm
ABCDE
1
2010869952209694721251000013754231724013110A1161007869952209694725100001375423240131A1161007
310236598456325634A
4
5010869952209694721x5Ay4231724013110A1161abc569856XX44EE8699522096947x5Ay423240131A1161abc569856XX44EE
Sheet1
 
Upvote 0
Solution
You're welcome. Glad it worked for you. Thanks for letting us know. :)

Might need to change the 'Mark as solution' post now though?
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,545
Members
449,316
Latest member
sravya

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