vba to extract numbers

Bartexcel

New Member
Joined
Sep 27, 2021
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
I need VBA to extract the numbers from a cell where specific alphas precede. example - big 1234 = 1234 or Big 1234 = 1234 or large 1234 = 1234 or grand 4321-01 = 4321-01 or Lg. 5678 = 5678.
 
It would be best if you could show us some different examples of what the data may really look like.
It is best not to oversimplify things when posting your questions, or else you may get answers that work for your oversimplified examples, but not for your real data.
sorry - i need vba or formula to find keywords like "large" then extract the number that follow see image. vba.PNG
 
Upvote 0
How about this?

Book2 (version 3).xlsb
ABCD
1DescriptionLargeMediumSmall
2On 9/29/2021 blah blah large 7684 at Orlando blah blah Med. 3255-01 to blah blah blah76843255-01#VALUE!
Sheet6
Cell Formulas
RangeFormula
B2B2=Xtract(A2,"large")
C2C2=Xtract(A2,"Med.")
D2D2=Xtract(A2,"small")


VBA Code:
Function Xtract(s As String, prefix As String)
With CreateObject("VBscript.RegExp")
    .Pattern = prefix & "\s(\d+\-?\d+)"
    Xtract = .Execute(s)(0).submatches(0)
End With
End Function
 
Last edited:
Upvote 0
If the text are always Large, Med and Small, you can use:

Book6
ABCDE
1DescriptionLargeMedSmall
2Friday Sept. 24th 2021, at Orlando station, 1 TRX container, large 7684 was used to ship customer ID 12121214 to Miami. Second shift requested 1 Carry-all, Med. 32555-01 to complete the transport order. First shift noted that Large container will not be enough for all of customers items. Shift manager noted that using the wrong size containers has caused 50% of transportation delays YTD. Small 12345768432555-0112345
3
Sheet1
Cell Formulas
RangeFormula
B2,D2B2=TRIM(LEFT(SUBSTITUTE(MID($A2,SEARCH(B$1,$A2)+LEN(B$1)+1,99)," ",REPT(" ",99)),99))
C2C2=TRIM(LEFT(SUBSTITUTE(MID($A2,SEARCH(C$1,$A2)+LEN(C$1)+2,99)," ",REPT(" ",99)),99))
 
Upvote 0
Is it always like: text - space - numbers?

VBA Code:
Sub jec()
 For Each it In Range("A1:A5")
    it.Offset(, 1).Value = Split(it)(1)
 Next
End Sub
This worked perfectly for my situation!

From this: AR Quiz No. 182818 EN Fiction
To this: 182818

Using VBA:

VBA Code:
For Each cell In rng
    Quiz = Split(cell.Value)(3)
Next cell
 
Upvote 0

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