Extract a number before specific letters & more....

SaulieDog

New Member
Joined
Apr 28, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
  2. Web
Hi Everyone -
I'm trying to extract the quantity number from the text AND if there isn't a quantity for it to return "1".
My end goal is to divide this number by a price to get the individual unit price.
As you can see sometimes it's at the beginning or end and may be followed by "pack or "/case"
I found some equations that can solve part of what I'm doing but can't figure out how to combine them correctly
I may be looking for the impossible. Any help or advice is appreciated.
Thanks very much.

PriceDescriptionUnitPrice/unit
$10010 pack, GAC post-carbon cartridge 1/4" JG black housing10$10
$40Storage Tank / 9 Gallon1$40
$600GAC Carbon Post Filter Cartridge, 3/8 JG, 50 PK50$12
$350RO Module, 30 gpd GE TFM-36A, 25/Case25$14
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Welcome to Mr Excel

Based upon the data that you have given us this, code will work but more data may identify
rows for which it does NOT work. If this the case then let me know.

Copy this code to a standard code module.

Test it on a copy of your data.

It asks you to select the range that you want to extract the quantity from.

VBA Code:
Public Sub subExtractQuantity()
Dim i As Integer
Dim arr() As String
Dim rngSelected As Range
Dim rng As Range
Dim varQty As Variant
Dim varValue As Variant

    ActiveWorkbook.Save
    
    On Error Resume Next
    Set rngSelected = Application.InputBox(Title:="Quantity extraction.", _
      Prompt:="Select the range of cells to extract quantity from.", Type:=8)
    On Error GoTo 0

    If rngSelected Is Nothing Then
        Exit Sub
    End If
    
    If rngSelected.Columns.Count > 1 Then
        Exit Sub
    End If
    
    For Each rng In rngSelected.Cells
    
        varValue = Replace(Replace(rng.Value, "/", ",", 1), " ", ",", 1)
        
        arr = Split(varValue, ",")
        
        If IsNumeric(arr(0)) Then
            varQty = arr(0)
        ElseIf IsNumeric(arr(UBound(arr) - 1)) Then
            varQty = arr(UBound(arr) - 1)
        Else
            varQty = 1
        End If
        
        rng.Offset(0, 1).Value = varQty
    
    Next rng
        
End Sub
 
Upvote 0
Hi & welcome to MrExcel.
A formula option
Fluff.xlsm
ABCD
1PriceDescriptionUnitPrice/unit
210010 pack, GAC post-carbon cartridge 1/4" JG black housing1010
340Storage Tank / 9 Gallon140
4600GAC Carbon Post Filter Cartridge, 3/8 JG, 50 PK5012
5350RO Module, 30 gpd GE TFM-36A, 25/Case2514
Master
Cell Formulas
RangeFormula
C2:C5C2=TEXTAFTER(TRIM(TEXTBEFORE(B2,{"pack"," PK","/case"},,1,,1))," ",-1,,1)
D2:D5D2=A2/C2
 
Upvote 0
Solution
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,328
Messages
6,124,299
Members
449,149
Latest member
mwdbActuary

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