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

topi1

Board Regular
Joined
Aug 6, 2014
Messages
161
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)
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try:
VBA Code:
Sub SplitFirstWord()
    Application.ScreenUpdating = False
    Dim fnd As Range, v As Variant, i As Long
    v = Range("A42", Range("A" & Rows.Count).End(xlUp)).Resize(, 2).Value
    For i = LBound(v) To UBound(v)
        Set fnd = Range("E42:E46").Find(Split(v(i, 1), " ")(0))
        If Not fnd Is Nothing Then
            Range("B" & i + 41) = Split(v(i, 1), " ")(0)
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
VBA Code:
Sub SplitFirstWord()
    Application.ScreenUpdating = False
    Dim fnd As Range, v As Variant, i As Long
    v = Range("A42", Range("A" & Rows.Count).End(xlUp)).Resize(, 2).Value
    For i = LBound(v) To UBound(v)
        Set fnd = Range("E42:E46").Find(Split(v(i, 1), " ")(0))
        If Not fnd Is Nothing Then
            Range("B" & i + 41) = Split(v(i, 1), " ")(0)
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
@mumps Thank you for your prompt help. Can you please help me with two issues?
1) It did not extract the rest. Column C remains empty.
2) recognize numbers as first word in case in the column E it has a cell that has 7 instead of seven. The current vba does not recognize 7. I don't know how to use excel formula TEXT for vba.
Thank you.
 
Upvote 0
Code.> Adjust the ranges as required.
VBA Code:
Sub GetFirstword()
Dim A, R, M, ary, Lr&, T&, Ta&
Lr = Range("A" & Rows.Count).End(xlUp).Row
A = Range("A3:A" & Lr)
ReDim R(1 To UBound(A, 1), 1 To 1)
ary = Range("E3:E7")

With CreateObject("scripting.dictionary")
For T = 1 To UBound(ary, 1)
.Add ary(T, 1), T
Next T

For Ta = 1 To UBound(A, 1)
M = Split(A(Ta, 1), " ")
If .exists(M(0)) Then R(Ta, 1) = M(0)
Next Ta
End With

Range("B3:B" & Lr) = R
End Sub
 
Upvote 0
Code.> Adjust the ranges as required.
VBA Code:
Sub GetFirstword()
Dim A, R, M, ary, Lr&, T&, Ta&
Lr = Range("A" & Rows.Count).End(xlUp).Row
A = Range("A3:A" & Lr)
ReDim R(1 To UBound(A, 1), 1 To 1)
ary = Range("E3:E7")

With CreateObject("scripting.dictionary")
For T = 1 To UBound(ary, 1)
.Add ary(T, 1), T
Next T

For Ta = 1 To UBound(A, 1)
M = Split(A(Ta, 1), " ")
If .exists(M(0)) Then R(Ta, 1) = M(0)
Next Ta
End With

Range("B3:B" & Lr) = R
End Sub
@kvsrinivasamurthy I got an error in the following line.
.Add ary(T, 1), T
 
Upvote 0
Try:
VBA Code:
Sub SplitFirstWord()
    Application.ScreenUpdating = False
    Dim fnd As Range, v As Variant, i As Long
    v = Range("A42", Range("A" & Rows.Count).End(xlUp)).Resize(, 2).Value
    For i = LBound(v) To UBound(v)
        Set fnd = Range("E42:E46").Find(Split(v(i, 1), " ")(0))
        If fnd Is Nothing Then
            Range("C" & i + 41) = v(i, 1)
        Else
            Range("B" & i + 41) = Split(v(i, 1), " ")(0)
            Range("C" & i + 41) = Split(v(i, 1), " ")(1)
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Try.
VBA Code:
Sub Macro1()
Dim A, R, M, ary, Lr&, T&, Ta&
Lr = Range("A" & Rows.Count).End(xlUp).Row
A = Range("A3:A" & Lr)
ReDim R(1 To UBound(A, 1), 1 To 1)
ary = Range("E3:E7")

With CreateObject("scripting.dictionary")
For T = 1 To UBound(ary, 1)
If ary(T, 1) <> "" Then .Add ary(T, 1), T
Next T

For Ta = 1 To UBound(A, 1)
If A(Ta, 1) <> "" Then
M = Split(A(Ta, 1), " ")
If .exists(M(0)) Then R(Ta, 1) = M(0)
End If
Next Ta
End With

Range("B3:B" & Lr) = R
End Sub
 
Upvote 0
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
 
Upvote 0
Try.
VBA Code:
Sub Macro1()
Dim A, R, M, ary, Lr&, T&, Ta&
Lr = Range("A" & Rows.Count).End(xlUp).Row
A = Range("A3:A" & Lr)
ReDim R(1 To UBound(A, 1), 1 To 1)
ary = Range("E3:E7")

With CreateObject("scripting.dictionary")
For T = 1 To UBound(ary, 1)
If ary(T, 1) <> "" Then .Add ary(T, 1), T
Next T

For Ta = 1 To UBound(A, 1)
If A(Ta, 1) <> "" Then
M = Split(A(Ta, 1), " ")
If .exists(M(0)) Then R(Ta, 1) = M(0)
End If
Next Ta
End With

Range("B3:B" & Lr) = R
End Sub
Sorry. It did not work. Didn't give an error message but didn't do anything to columns B or C.
 
Upvote 0
Try:
VBA Code:
Sub SplitFirstWord()
    Application.ScreenUpdating = False
    Dim fnd As Range, v As Variant, i As Long
    v = Range("A42", Range("A" & Rows.Count).End(xlUp)).Resize(, 2).Value
    For i = LBound(v) To UBound(v)
        Set fnd = Range("E42:E46").Find(Split(v(i, 1), " ")(0))
        If fnd Is Nothing Then
            Range("C" & i + 41) = v(i, 1)
        Else
            Range("B" & i + 41) = Split(v(i, 1), " ")(0)
            Range("C" & i + 41) = Split(v(i, 1), " ")(1)
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
@mumps 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

Forum statistics

Threads
1,215,123
Messages
6,123,182
Members
449,090
Latest member
bes000

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