VBA code to split the data as per requirement based on key words

Wafee

Board Regular
Joined
May 27, 2020
Messages
104
Office Version
  1. 2013
Platform
  1. Windows
Hi,
Can someone help we with a code for below scenario.

I have data in A column and need to split it to B, C, D Columns. below is possible formats of data.

1. If it contains data with "AB" then it has to pull the 8 digit alpha numeric (8 numbers + 5 alphabets) ID and paste it in B column cell
2. If it contains data with "FG" then it has to pull the 8 digit alpha numeric ID and paste it in C column cell
3. If it containd data "Ver" then number associated with "Ver" is to be pasted in D column cell. "ver1" and '(ver, 3)" are the only possible formats.
4. If it neither has above three then we can ignore it.
5. if it is blank then ignore it.

thank you in advance.


Column - AColumn B - AB IDColumn C - FG IDColumn D - Version
SINAB76542658 - Ver1SINAB765426581
SINAB76390659 - (Ver, 3)SINAB763906593
SINAB76327645SINAB76327645
SINFG76542658 - Ver11SINFG7654265811
SINFG76542376 - (Ver, 7)SINFG765423767
75638jhm
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Is "AB" or "FG" (if they exist) always in positions 4 & 5 of the text like your examples?

Are you sure this needs to be done with vba as I believe it could be done with formulas based on the samples so far?
 
Upvote 0
Y
Is "AB" or "FG" (if they exist) always in positions 4 & 5 of the text like your examples?

Are you sure this needs to be done with vba as I believe it could be done with formulas based on the samples so far?
Yes mate, they will be always in position 4&5. This is needs to in VBA as it is one part of my automation.
 
Upvote 0
they will be always in position 4&5.
Thanks for the clarification. Try this with a copy of your data.

VBA Code:
Sub SplitToColumns()
  Dim a As Variant, b As Variant
  Dim i As Long
  Dim s As String
 
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 3)
  For i = 1 To UBound(a)
    s = a(i, 1)
    Select Case True
      Case Mid(s, 4, 2) = "AB"
        b(i, 1) = Left(s, 13)
      Case Mid(s, 4, 2) = "FG"
        b(i, 2) = Left(s, 13)
    End Select
    If InStr(14, s, "ver", vbTextCompare) > 0 Then b(i, 3) = StrReverse(Mid(Val(StrReverse(Replace(s, ")", "") & 1)), 2))
  Next i
  Range("B2:D2").Resize(UBound(b)).Value = b
End Sub


My sample data and results:

Wafee.xlsm
ABCD
1Column - AColumn B - AB IDColumn C - FG IDColumn D - Version
2SINAB76542658 - Ver1SINAB765426581
3SINAB76390659 - (Ver, 3)SINAB763906593
4SINAB76327645SINAB76327645
5SINFG76542658 - Ver11SINFG7654265811
6SINFG76542376 - (Ver, 7)SINFG765423767
775638jhm
Sheet1
 
Upvote 0
Solution
Thanks for the clarification. Try this with a copy of your data.

VBA Code:
Sub SplitToColumns()
  Dim a As Variant, b As Variant
  Dim i As Long
  Dim s As String

  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 3)
  For i = 1 To UBound(a)
    s = a(i, 1)
    Select Case True
      Case Mid(s, 4, 2) = "AB"
        b(i, 1) = Left(s, 13)
      Case Mid(s, 4, 2) = "FG"
        b(i, 2) = Left(s, 13)
    End Select
    If InStr(14, s, "ver", vbTextCompare) > 0 Then b(i, 3) = StrReverse(Mid(Val(StrReverse(Replace(s, ")", "") & 1)), 2))
  Next i
  Range("B2:D2").Resize(UBound(b)).Value = b
End Sub


My sample data and results:

Wafee.xlsm
ABCD
1Column - AColumn B - AB IDColumn C - FG IDColumn D - Version
2SINAB76542658 - Ver1SINAB765426581
3SINAB76390659 - (Ver, 3)SINAB763906593
4SINAB76327645SINAB76327645
5SINFG76542658 - Ver11SINFG7654265811
6SINFG76542376 - (Ver, 7)SINFG765423767
775638jhm
Sheet1
That works perfectly mate. Thank you Genius :-)
 
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,614
Members
452,661
Latest member
Nonhle

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