Extracting words based on two initial characters

Deepk

Board Regular
Joined
Mar 21, 2018
Messages
105
Office Version
  1. 2016
Platform
  1. Windows
Hi team,

I have some entries in cells of a column. See example below
A
1 AB67436C1 | SR7487OP | KW465875QP | LO68782XE | SR5743257TA

2 LO68782XE | KW465875QP | AB67436C1 | SR465456YA | BV47586TR

3 DZ7645657UO

I want to have a macro that extract the individual entries based on the the first two alphabets. The macro should work in the following manner.

It ask me to enter (in an inputbox) the list of two alphabets, like "SR, LO, BV".
ask me to enter (in second inputbox) the column number in which the extracted data should be pasted. Like "4".
It gives output in the 4th column like below

D
1 SR7487OP | LO68782XE | SR5743257TA

2 LO68782XE | SR465456YA | BV47586TR

3 Not Available

Looking forward in anticipation!

Thank you.
 
Ok so i assume the original data is not in column A. I modified it (and tried it), now it should go in the right column.

Code:
Sub RefineFamily()

 Dim alpha, strBuffer As String
 Dim col As Double
 Dim refList As Variant
 Dim c As Range
 Dim i As Integer
 On Error GoTo ErrHandling
 alpha = InputBox("Choose Country Codes", "Prompt")
 col = CDbl(InputBox("Choose a column number", "Prompt"))
 ' For Each c In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
     For Each c In Selection
    
         refList = Split(Replace(c.Value, " ", ""), "|")
        
         For Each ref In refList
        
         If InStr(1, alpha, Left(ref, 2)) > 0 Then
         strBuffer = strBuffer & ref & " | "
         i = i + 1
         End If
        
         Next ref
        
         If i > 0 Then
            Cells(c.Row, col).Value = Left(strBuffer, Len(strBuffer) - 3)
         Else
            Cells(c.Row, col).Value = "Not Found"
         End If
        
         i = 0
        
         strBuffer = ""
        
     Next c
ErrHandling:
 If Err.Number <> 0 Then
MsgBox Prompt:="Please verify the data your entered", Title:="Prompt Error"
Else
 MsgBox "Process Completed Successfully."
End If
 End Sub

hi louisH and team,

Hope you are doing good.

I want a slight modification in the above code. I want to feed a list of two initial characters like

Array= (AB, BV, CA.... and so on)

If someone enters two initial characters (in the inputbox) other than the characters provided in the list, an error msgbox = "Check your input!" will appear and it exit the macro.

Please have a look and update the code. Thank you in advance.
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Forum statistics

Threads
1,216,093
Messages
6,128,784
Members
449,468
Latest member
AGreen17

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