Split first word only if it is part of a range.

topi1

Active Member
Joined
Aug 6, 2014
Messages
252
Office Version
  1. 2010
In the following example, I have a range of values in the column E. data in the column A. In the columns B and C I have entered formulas (arrived by working stepwise on several columns and then combining it). Everything works well and extracts first word from A into column B only if it is part of the range in the column E. And it leaves the rest in the column C.
Is it possible to have a vba which can do the same thing? Thank you.

rscripto11.xlsm
ABCDE
42GM #1 GM #1One
43Dodge #4 Dodge #4Two
44Ford #2 Ford #2Three
45Jeep #3 Jeep #3Four
46One DodgeOneDodgeFive
47Two DodgeTwoDodge
48Two JeepTwoJeep
49One DodgeOneDodge
50One JeepOneJeep
51Two JeepTwoJeep
Sheet6
Cell Formulas
RangeFormula
B42:B51B42=IF((COUNTIF($E$42:$E$51,IFERROR(LEFT(A42, FIND(" ", A42)-1), A42))=1),IFERROR(LEFT(A42, FIND(" ", A42)-1), A42),"")
C42:C51C42=IF((COUNTIF($E$42:$E$51,IFERROR(LEFT(A42, FIND(" ", A42)-1), A42))=1),IF((ISERROR(SUBSTITUTE(A42,LEFT(A42,FIND(" ",A42)),""))=FALSE),SUBSTITUTE(A42,LEFT(A42,FIND(" ",A42)),""),""),A42)
 
VBA Code:
Sub jec()
 Dim ar, ar2, j As Long, jj As Long
 ar = Range("A42", Range("A" & Rows.Count).End(xlUp)).Resize(, 3)
 ar2 = Range("E42:E46")
 For j = 1 To UBound(ar)
   ar(j, 3) = ar(j, 1)
   For jj = 1 To UBound(ar2)
     If InStr(ar(j, 1), ar2(jj, 1)) Then
       ar(j, 2) = Split(ar(j, 1))(0)
       ar(j, 3) = Split(ar(j, 1))(1)
       Exit For
     End If
   Next
 Next
 Range("A42", Range("A" & Rows.Count).End(xlUp)).Resize(, 3) = ar
End Sub
@JEC It works great as long the value in E is not a number. Is there a solution? I tried to add some columns and tried to use TEXT formula, but it did not work. TY.
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Can you post a new sheet that has some numbers?
 
Upvote 0
Untested, try to use this line instead

VBA Code:
If InStr(ar(j, 1), Cstr(ar2(jj, 1))) then
 
Upvote 0
Can you post a new sheet that has some numbers?
I am so sorry. As I was creating XL2BB file, I realized that I had to extend the range in column E. Now it works like a charm. Even with numbers. Sorry about such stupid oversight and wasting yours and JEC's time. Thank you so much.
 
Upvote 0
You are very welcome. Glad we could help. :)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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