How to extract the middle of a string?

thewiseguy

Well-known Member
Joined
May 23, 2005
Messages
956
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have a long list of products which contain multiple pieces of information in the string. I need to "clean up" the string of text and so I only need some of the information to be displayed.

Here is an example of the data that needs to be cleaned: Maxlite 100W LED T2 Architectural AreaLight 4K 120/277 Slip Fitter Mount

Here is what I would like the output to be: 100W LED T2 Architectural AreaLight

Each line of data is some variation of this. The first word is ALWAYS the manufacturer and can be removed.
Each line of data always has some variation of "4K." It could be 35K, 4K, 65K, but it will always end in "K."

Is there a formula I can use to clean up a few hundred lines of data as shown above?

Many thanks in advance!
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi thewiseguy,

Using the nifty formula by barry houdini here try this (just change the initial cell reference from A2 to where your data starts if need be):

=MID(LEFT(A2,FIND("^^",SUBSTITUTE(A2," ","^^",4))-1),FIND("^^",SUBSTITUTE(A2," ","^^",1))+1,LEN(LEFT(A2,FIND("^^",SUBSTITUTE(A2," ","^^",4))-1)))

Regards,

Robert
 
Upvote 0
Actually try this:

=MID(LEFT(A2,FIND("^^",SUBSTITUTE(A2," ","^^",6))-1),FIND("^^",SUBSTITUTE(A2," ","^^",1))+1,LEN(LEFT(A2,FIND("^^",SUBSTITUTE(A2," ","^^",6))-1)))
 
Upvote 0
Hi,

Assuming the "manufacturer" is Always just a Single word at the beginning (if not, I'll adjust the formula to suit).
Also, that the "K" will not exceed 2 digits (e.g. not 100K)

Try this:

Book3.xlsx
AB
1Maxlite 100W LED T2 Architectural AreaLight 4K 120/277 Slip Fitter Mount100W LED T2 Architectural AreaLight
2Maxlite 40W LED T9 Architectural AreaLight 65K 120/277 Slip Fitter Mount40W LED T9 Architectural AreaLight
Sheet908
Cell Formulas
RangeFormula
B1:B2B1=TRIM(MID(SUBSTITUTE(LEFT(A1,SEARCH("?K ",A1)-2)," ",REPT(" ",LEN(A1)),1),LEN(A1),LEN(A1)))
 
Upvote 0
Actually try this:

=MID(LEFT(A2,FIND("^^",SUBSTITUTE(A2," ","^^",6))-1),FIND("^^",SUBSTITUTE(A2," ","^^",1))+1,LEN(LEFT(A2,FIND("^^",SUBSTITUTE(A2," ","^^",6))-1)))
So this worked perfectly, except for a few values:

Original Value:


Actually try this:

=MID(LEFT(A2,FIND("^^",SUBSTITUTE(A2," ","^^",6))-1),FIND("^^",SUBSTITUTE(A2," ","^^",1))+1,LEN(LEFT(A2,FIND("^^",SUBSTITUTE(A2," ","^^",6))-1)))


So this worked perfectly with the exception of a few values:

ORIGINAL VALUE: Maxlite 28W LED Adjustable WallPack 4K 120/277
OUTPUT: 28W LED Adjustable WallPack 4K
ERROR: The "4K" should not be visible

ORIGINAL VALUE: Maxlite 200W LED Xlarge Flood 4K 120/277 Slip Fitter Mount
OUTPUT: 200W LED Xlarge Flood 4K
ERROR: The "4K" should not be visible

I wish I could decipher the formula you provided but it is a bit above my pay grade. Any ideas?

Thank you again.
 
Upvote 0
Hi
What about VBA
VBA Code:
Sub test()
    Dim a As Variant
    Dim i As Long
    a = Cells(1, 1).Resize(Cells(Rows.Count, 1).End(xlUp).Row)
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "(^\w+) |(\d+[kK].+)"
        For i = 1 To UBound(a)
     a(i, 1) = .Replace(a(i, 1), "")
        Next
    End With
    Cells(1, 2).Resize(UBound(a)) = a
End Sub
 
Upvote 0
Any ideas?

I would actually use this slight variation of jtakw's nifty suggestion:

=TRIM(MID(SUBSTITUTE(LEFT(A2,FIND("K",A2)-3)," ",REPT(" ",LEN(A2)),1),LEN(A2),LEN(A2)))

If the output for all cases is still not correct then mohadin's VBA solution might be the way to go as there can be so many variations with data formulas can become cumbersome and complex trying to get consistent results.

HTH

Robert
 
Upvote 0
Another option if you have the LET function
+Fluff 1.xlsm
AB
1
2Maxlite 100W LED T2 Architectural AreaLight 4K 120/277 Slip Fitter Mount100W LED T2 Architectural AreaLight
3Maxlite 40W LED T9 Architectural AreaLight 65K 120/277 Slip Fitter Mount40W LED T9 Architectural AreaLight
4Maxlite 28W LED Adjustable WallPack 4K 120/27728W LED Adjustable WallPack
5Maxlite 2000W LED Xlarge Flood 4K 120/277 Slip Fitter Mount2000W LED Xlarge Flood
6Maxlite 9W LED Xlarge Flood 400K 120/277 Slip Fitter Mount9W LED Xlarge Flood
7Max lite 28W LED Adjustable WallPack 4K 120/27728W LED Adjustable WallPack
Test
Cell Formulas
RangeFormula
B2:B7B2=LET(Txt,SUBSTITUTE(A2," ",REPT(" ",100)),TRIM(MID(LEFT(Txt,FIND("K ",Txt)-20),FIND("W ",Txt)-20,LEN(Txt))))
 
Last edited:
Upvote 0
ThIs UDF code works
VBA Code:
Function ExtractData(Ip As String)
Dim M, K


K = False
M = Split(Ip, " ")


For T = 0 To UBound(M)
If InStr(1, M(T), "W") > 0 Then
K = True
ElseIf InStr(1, M(T), "K") > 0 Then
Exit For
End If


If K = True Then Temp = Temp & " " & M(T)
Next T


If Temp <> "" Then ExtractData = Mid(Temp, 2) Else ExtractData = ""
End Function

UDF

How to Use UDF code:
In the developer tab click--> Visual Basic
VB window opens
Insert--> Module
Paste the code.
Close the VB window.
Now UDF is available in Function List

Say A2 is having data.
In B2 formula

=ExtractData(A2)
 
Upvote 0
One more UDF...
VBA Code:
Function ExtractPart(ByVal vR As Range)
  
    Dim vA, vA2()

    vA = Split(vR, " ")
    ReDim vA2(5)
    For x = 1 To 5
        vA2(x) = vA(x)
    Next x
    ExtractPart = Trim(Join(vA2, " "))

End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,084
Messages
6,128,722
Members
449,465
Latest member
TAKLAM

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