Macro for extracting data

sparkerk1

New Member
Joined
Nov 1, 2017
Messages
18
Office Version
  1. 2016
Platform
  1. Windows
hi

i have below sample data , from which i want to extract only certain value only

5 TK 468 M 06JUL 1 ODSIST HK4 2140 2305 06JUL E TK/VGRWLL

Output required

TK 468 06JUL ODSIST 2140 2305 06JUL TK/VGRWLL
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi sparkerk,

With the very limited information supplied & only 1 example to work with, give the below code a try ...

VBA Code:
Sub test()

Dim c As Range, j$

For Each c In Range("A2", Range("A" & Rows.Count).End(3)) '<-- change range as needed
   For x = 1 To 12
      Select Case x
         Case 1, 2, 4, 6, 8 To 10, 12: j = IIf(Len(j) = 0, Split(c)(x), j & " " & Split(c)(x))
      End Select
   Next
   c = j: j = vbNullString
Next

End Sub
 
Upvote 0
hi

Please find some examples for reference

Eg 1

PT3U5G
BOMVS316E/2005NM/3JUL20
1.DUBEY/PRASHANT MR
2 QR 743 K 03JUL 5 DOHBOS HK1 0800 1415 03JUL E QR/PT3U5

Eg 2

MSPSGM
BOMVS316E/2005NM/2JUL20
1.SAURAV/SAJAL MR 2.SUVARNA/RAHUL MR 3.VERMA/DEEPAK MR
4 QR 729 K 10JUL 5*DOHDFW HK3 0750 1530 10JUL E QR/MSPSGM
5 QR2720 K 10JUL 5*DFWIAH HK3 1855 2008 10JUL E QR/MSPSGM


For Eg1 tried ur code got below result


2 QR K 5 DOHBOS HK1 0800


Data should be extracted from only the lines mentioned in line 2 of Eg 1 and line 4 and 5 of eg 2,

output required should be as below

for eg 1

QR 743 03JUL DOHBOS 0800 1415 03JUL QR/PT3U5


for eg 2


QR 729 10JUL DOHDFW 0750 1530 10JUL QR/MSPSGM
QR2720 10JUL DFWIAH 1855 2008 10JUL QR/MSPSGM
 
Upvote 0
OK, try the revised code ... I think if you have more patterns then a RegEx is required

VBA Code:
Sub test()

Dim c As Range, j$
For Each c In Range("A2", Range("A" & Rows.Count).End(3)) '<-- change range as needed
   If UBound(Split(c)) >= 10 Then
      c.Replace "~*", " "
      If c Like "? ?? ###*" Then c.Value = Left(c, 4) & "|" & Mid(c, 6)
         For x = 1 To 11
            Select Case x
               Case 1, 3, 5, 7 To 9, 11: j = IIf(Len(j) = 0, Split(c)(x), j & " " & Split(c)(x))
            End Select
         Next
      c = j: j = vbNullString
      c.Replace "|", " "
   End If
Next

End Sub

Book1
ABC
1InputExpected OutputCheck
2PT3U5GPT3U5GTRUE
3BOMVS316E/2005NM/3JUL20BOMVS316E/2005NM/3JUL20TRUE
41.DUBEY/PRASHANT MR1.DUBEY/PRASHANT MRTRUE
52 QR 743 K 03JUL 5 DOHBOS HK1 0800 1415 03JUL E QR/PT3U5QR 743 03JUL DOHBOS 0800 1415 03JUL QR/PT3U5FALSE
6TRUE
7MSPSGMMSPSGMTRUE
8BOMVS316E/2005NM/2JUL20BOMVS316E/2005NM/2JUL20TRUE
91.SAURAV/SAJAL MR 2.SUVARNA/RAHUL MR 3.VERMA/DEEPAK MR1.SAURAV/SAJAL MR 2.SUVARNA/RAHUL MR 3.VERMA/DEEPAK MRTRUE
104 QR 729 K 10JUL 5*DOHDFW HK3 0750 1530 10JUL E QR/MSPSGMQR 729 10JUL DOHDFW 0750 1530 10JUL QR/MSPSGMFALSE
115 QR2720 K 10JUL 5*DFWIAH HK3 1855 2008 10JUL E QR/MSPSGMQR2720 10JUL DFWIAH 1855 2008 10JUL QR/MSPSGMFALSE
12TRUE
135 TK 468 M 06JUL 1 ODSIST HK4 2140 2305 06JUL E TK/VGRWLLTK 468 06JUL ODSIST 2140 2305 06JUL TK/VGRWLLFALSE
Sheet2
Cell Formulas
RangeFormula
C2:C13C2=A2=B2
 
Upvote 0
hi

tried your above code didn't work

Example:

PPEP7N
BOMVS316E/1133KM/7JUL20
1.ASHISH BOLA/MR 2.KAUSHAL/ABNISH MR 3.PINTO/DERYL MR
4 QR 067 K 06JUL 1 DOHFRA HK3 0755 1330 06JUL E QR/PPEP7N
5 LH 026 M 06JUL 1 FRAHAM HK3 1730 1835 06JUL E LH/PPEP7N

getting below results

PPEP7N
BOMVS316E/1133KM/7JUL20
BOLA/MR MR MR
067 06JUL 1 DOHFRA
026 06JUL 1 FRAHAM
 
Upvote 0
Hi,

I have tried the code in post #4 and I get different results than the one you posted ? ... This is what I get

Book1
AB
1InputOutput
2PPEP7NPPEP7N
3BOMVS316E/1133KM/7JUL20BOMVS316E/1133KM/7JUL20
41.ASHISH BOLA/MR 2.KAUSHAL/ABNISH MR 3.PINTO/DERYL MR1.ASHISH BOLA/MR 2.KAUSHAL/ABNISH MR 3.PINTO/DERYL MR
54 QR 067 K 06JUL 1 DOHFRA HK3 0755 1330 06JUL E QR/PPEP7NQR 067 06JUL DOHFRA 0755 1330 06JUL QR/PPEP7N
65 LH 026 M 06JUL 1 FRAHAM HK3 1730 1835 06JUL E LH/PPEP7NLH 026 06JUL FRAHAM 1730 1835 06JUL LH/PPEP7N
Sheet1
 
Upvote 0
hi i have made a file , kindly how can i attach the same here so u can have look and advise me where i am going wrong
 
Upvote 0
You can’t attach files .. Upload the file in DropBox (or similar cloud folder) & share the link with us
 
Upvote 0
Well, based on your excel file, there are some leading/trailing & double spacing in the data. I have added one line to take care of that. Give it a try now

Note: The code will NOT work for this example [ 3 VR 053 Y 11JUL 6 DOHDEL GK1 0800 1405 11JUL SUKANYA] as it is missing the letter after the date 11JUL which is another pattern

Rich (BB code):
Sub test()

Dim c As Range, j$
For Each c In Range("A2", Range("A" & Rows.Count).End(3)) '<-- change range as needed
   If UBound(Split(c)) >= 10 Then
      c.Replace "~*", " "
      c = Application.Trim(c)
      If c Like "? ?? ###*" Then c.Value = Left(c, 4) & "|" & Mid(c, 6)
         For x = 1 To 11
            Select Case x
               Case 1, 3, 5, 7 To 9, 11: j = IIf(Len(j) = 0, Split(c)(x), j & " " & Split(c)(x))
            End Select
         Next
      c = j: j = vbNullString
      c.Replace "|", " "
   End If
Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,684
Members
449,116
Latest member
HypnoFant

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