find p- and split before it to next cell

shrinivasmj

Board Regular
Joined
Aug 29, 2012
Messages
140
i need a vba code

its in cell b1 split to cell c2

find p- and split before it to next cell

CLI-65389 CLI-OvQ-Jmn_65389 P-805183301131393 RMLCEOSHGHMVKTNB Western Mutual Insurance Group B6ZT9YJ7 Protection Plans LI2542168 475 2 Year YVONNE M NELSON 460 W SCHULTE RD ATLANTA CA 90136 (773) 224-9209 3
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
So you want this split out..."CLI-65389 CLI-OvQ-Jmn_65389" ?

If so:

=TRIM(LEFT(B1,FIND("P-",B1)-1))

It will work with or without the TRIM around it, but just for good measure... :)
 
Upvote 0
You could just drag that formula down, but....Ok, this is totally untested because I'm at home and no excel t test on, but I'll give it a shot.

Code:
Dim MyRange as Range
Dim Cell as Range

MyRange = Range("[B][COLOR=#ff0000]B1:B100[/COLOR][/B]")

For Each Cell in MyRange
    Cell.Offset(1,1).Value = Left(Cell.Value,Instr(Cell.Value,"P-"))
Next Cell

Change what I have in red to the Range your data is in.

This will put the left characters until it finds "P-" one cell to the right and one cell down from the original data (you said to put B1 into C2...not my idea).

PLEASE, PLEASEEEEEE test it on a COPY of your workbook, as it will not be able to be undone if I got something wrong. As I said, it is completely untested and written totally from memory.

You may have to change this:
Code:
Cell.Offset(1,1).Value = Left(Cell.Value,Instr(Cell.Value,"P-"))
to this:
Code:
Cell.Offset(1,1).Value = Left(Cell.Value,Instr(Cell.Value,"P-")[B][COLOR=#0000cd]-1[/COLOR][/B])
or some other number to get what you want.
 
Last edited:
Upvote 0
If your question has not been resolved, could you please give 4-5 samples of data AND the expected results?
Also, please specify exactly where the data is and where then results should go.
 
Upvote 0
this is the data below

in cell B:B
AMI-77561 AMI-OvQ-Jmn_77561 P-751502213743641 IIJH7NQSP58AGIZB
CLI-77561 CLI-OvQ-Jmn_77561 P-320497742131143 YAZZXGW02B8B40YA
HI-77561 HI-OvQ-Jmn_77561 P-210686976842370 CRDN69JJ4HH4EM7U

OUT PUT I NEED

CELL B;B---AMI-77561 AMI-OvQ-Jmn_77561 CELL C ---P-751502213743641 IIJH7NQSP58AGIZB
CELL B;B---CLI-77561 CLI-OvQ-Jmn_77561 CELL C --- P-320497742131143 YAZZXGW02B8B40YA
CELL B;B---HI-77561 HI-OvQ-Jmn_77561 CELL C --- P-210686976842370 CRDN69JJ4HH4EM7U
 
Last edited:
Upvote 0
Try
Code:
Sub Split_Text()
  With Range("B1", Range("B" & Rows.Count).End(xlUp))
    .Offset(, 1).Value = Evaluate(Replace("if(row(#),replace(#,1,search("" P-"",#&"" P-""),""""),"""")", "#", .Address))
    .Value = Evaluate(Replace("if(row(#),left(#,search("" P-"",#&"" P-"")-1),"""")", "#", .Address))
  End With
End Sub
 
Upvote 0
Try
Code:
Sub Split_Text()
  With Range("B1", Range("B" & Rows.Count).End(xlUp))
    .Offset(, 1).Value = Evaluate(Replace("if(row(#),replace(#,1,search("" P-"",#&"" P-""),""""),"""")", "#", .Address))
    .Value = Evaluate(Replace("if(row(#),left(#,search("" P-"",#&"" P-"")-1),"""")", "#", .Address))
  End With
End Sub


in cell c:c its showing value
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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