Find value between 2 characters

stroffso

Board Regular
Joined
Jul 12, 2016
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have a nicely aligned workbook but there is one small part I am trying to figure out.

Our Product unit numbers go from 1-50 however they are all within a string for that product code. I know how to get figures to the left and the right etc but I want to get figures in between certain values.

Example of the strings I am using:

Levant [1 - 304554-3022]
Levant [18 - 304554-3022]
Hobson [24 - 304554-3022]

So in the above 3 cases the values returned would be
1
18
24

Simply, Anything after the [ but before the first -

All help greatly appreciated

thanks
 
Last edited:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
You can do this with PowerQuery.

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Split Column by Delimiter" = Table.SplitColumn(Source, "Column1", Splitter.SplitTextByDelimiter("[", QuoteStyle.Csv), {"Column1.1", "Column1.2"}),
    #"Split Column by Delimiter1" = Table.SplitColumn(#"Split Column by Delimiter", "Column1.2", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Column1.2.1", "Column1.2.2", "Column1.2.3"}),
    #"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter1",{"Column1.1", "Column1.2.2", "Column1.2.3"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Column1.2.1", "Product Code"}}),
    #"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"Product Code", Int64.Type}})
in
    #"Changed Type"
 
Upvote 0
Here's a VBA solution as well. This code assumes that your data begins in A1, and it will output the results to column C.

Code:
Sub PRODUCTNUMBER()
Dim SP() As Variant: SP = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row()).Value
Dim AL As Object: Set AL = CreateObject("System.Collections.ArrayList")

For i = LBound(SP) To UBound(SP)
    AL.Add (Trim(Split(Right(SP(i, 1), Len(SP(i, 1)) - InStr(SP(i, 1), "[")), " ")(0)))
Next i

Range("C1").Resize(AL.Count, 1).Value = Application.Transpose(AL.toarray)

End Sub
 
Last edited:
Upvote 0
Thanks so much for that I thought there would be a way with a formula but will run it with the VBA as I am not too up on Power query but thank you both for your solutions

Here's a VBA solution as well. This code assumes that your data begins in A1, and it will output the results to column C.

Code:
Sub PRODUCTNUMBER()
Dim SP() As Variant: SP = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row()).Value
Dim AL As Object: Set AL = CreateObject("System.Collections.ArrayList")

For i = LBound(SP) To UBound(SP)
    AL.Add (Trim(Split(Right(SP(i, 1), Len(SP(i, 1)) - InStr(SP(i, 1), "[")), " ")(0)))
Next i

Range("C1").Resize(AL.Count, 1).Value = Application.Transpose(AL.toarray)

End Sub
 
Upvote 0
This formula seems to work too,
Code:
=TRIM(MID(A1,FIND("[",A1)+1,FIND(" ",A1,FIND("[",A1))-FIND("[",A1)))
 
Last edited:
Upvote 0
Here is a formula solution...

=MID(TRIM(LEFT(A1,FIND("-",A1)-1)),FIND("[",A1)+1,2)

Since your product numbers run from 1 to 50 (max of 2 digits), I was able to use that fact in the MID function.

By the way, we can use this formula as the basis of a VBA solution...
Code:
[table="width: 500"]
[tr]
	[td]Sub GetNumber()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Range("C1:C" & LastRow) = Evaluate(Replace("IF({1},MID(TRIM(LEFT(A1:A#,FIND(""-"",A1:A#)-1)),FIND(""["",A1:A#)+1,2))", "#", LastRow))
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Also maybe a formula

Row\Col
A​
B​
1​
Levant [1 - 304554-3022]=MID(LEFT(A1,FIND(" -",A1)-1),FIND("[",A1)+1,LEN(A1))
2​
Levant [18 - 304554-3022]=MID(LEFT(A2,FIND(" -",A2)-1),FIND("[",A2)+1,LEN(A2))
3​
Hobson [24 - 304554-3022]=MID(LEFT(A3,FIND(" -",A3)-1),FIND("[",A3)+1,LEN(A3))
 
Upvote 0
@post #2
enough one line with:
Code:
Table.TransformColumns(Source, {{"raw", each Text.BetweenDelimiters(_, "[", " "), type text}})
 
Upvote 0
Hi,

Another formula solution, if the numbers are Always 2 digits (i.e. between 1 to 50), why can't it be just:


Book1
ABC
1Levant [1 - 304554-3022]11
2Levant [18 - 304554-3022]1818
3Hobson [24 - 304554-3022]2424
Sheet674
Cell Formulas
RangeFormula
B1=MID(A1,FIND("[",A1)+1,2)+0
C1=TRIM(MID(A1,FIND("[",A1)+1,2))


Formulas copied down.
B1 formula converts results to Real Numbers.
C1 formula results as Text.
 
Upvote 0
...if the numbers are Always 2 digits (i.e. between 1 to 50), why can't it be just:


Book1
ABC
1Levant [1 - 304554-3022]11
2Levant [18 - 304554-3022]1818
3Hobson [24 - 304554-3022]2424
Sheet674
Cell Formulas
RangeFormula
B1=MID(A1,FIND("[",A1)+1,2)+0
C1=TRIM(MID(A1,FIND("[",A1)+1,2))
But of course... :oops:
 
Upvote 0

Forum statistics

Threads
1,215,353
Messages
6,124,458
Members
449,161
Latest member
NHOJ

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