Extract number(s) from A String after specific phrase to an Array

airforceone

Board Regular
Joined
Feb 14, 2022
Messages
177
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
from my previous problem, i somehow micro-solved it up until this problem.
from the entry below, i would like to extract the grams and insert it below corresponding cell and store the Amount of said string to array, using VBA.

i.e.
for cell B2
1.001 grams will go to B3 as 1.001
while the Amount will be stored to Array using the Branch Control Number as "KEY"
 

Attachments

  • 2022.02.20 Mr. Excel.JPG
    2022.02.20 Mr. Excel.JPG
    63.9 KB · Views: 15

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
from my previous problem, i somehow micro-solved it up until this problem.
from the entry below, i would like to extract the grams and insert it below corresponding cell and store the Amount of said string to array, using VBA.

i.e.
for cell B2
1.001 grams will go to B3 as 1.001
while the Amount will be stored to Array using the Branch Control Number as "KEY"

Hi There,

Can you send the fake sample (in case this is a credential file) that what you expected?

The question still not completely clear :):coffee:
 
Upvote 0
Hi There,

Can you send the fake sample (in case this is a credential file) that what you expected?

The question still not completely clear :):coffee:

thanks for finding time to view my problem, anyway here is what I have so far. What I'm praying is to put this formula to VBA/UDF.
sample sheet as per requested mate :)

00 Updating Bravo UPDATED.xlsx
ABCDE
1Branch Control NumberVitaminsVitaminsVitaminsVitamins
2A00001-2022-0001Vit A Grams : 1.001 Amount : 1Zinc Grams : 0.1 Amount : 10Vit D Grams : 0.01 Amount : 100Vit D Grams : 0.0001 Amount : 1000
31.0010.10.010.0001
4A00001-2022-0002Vit A Grams : 1.001 Amount : 1Zinc Grams : 0.1 Amount : 10Vit D Grams : 0.01 Amount : 100Vit D Grams : 0.0001 Amount : 1000
51.0010.10.010.0001
6
7using this Formula I can extract the Integer Value of a String but not in VBA
8=MID(B2,SEARCH(": ",B2)+LEN(": "),SEARCH("Amount",B2)-SEARCH(": ",B2)-LEN(": "))
Sheet1 (2)
Cell Formulas
RangeFormula
B5:E5,B3:E3B3=MID(B2,SEARCH(": ",B2)+LEN(": "),SEARCH("Amount",B2)-SEARCH(": ",B2)-LEN(": "))

while the amount value goes to an array
 
Upvote 0
perhaps something along the lines of this
VBA Code:
Function getgrams(sStr As String)
    getgrams = (Split(Split(sStr, "Grams : ")(1), " ")(0)) * 1
End Function
 
Upvote 0
Solution
perhaps something along the lines of this
VBA Code:
Function getgrams(sStr As String)
    getgrams = (Split(Split(sStr, "Grams : ")(1), " ")(0)) * 1
End Function
good day mate, thanks for taking time to piece a code for the problem
would you be kind enough to explain how to use your function in a range loop? say we have 800 records in a range
I already have a working code, but your code seems more simpler than mine (might be faster) I really love to hear from you...
 
Upvote 0
good day mate, thanks for taking time to piece a code for the problem
would you be kind enough to explain how to use your function in a range loop? say we have 800 records in a range
I already have a working code, but your code seems more simpler than mine (might be faster) I really love to hear from you...
All is Good now!
I incorporate you code in my routines and manage to tweak it a bit now is working... for reference to others with similar problem here's my code along with yours

Cells(x, 2 + k).Value = "=MID(R[-" & xVARx & "]C,SEARCH("": "",R[-" & xVARx & "]C)+LEN("": ""),SEARCH(""Value"",R[-" & xVARx & "]C)-SEARCH("": "",R[-" & xVARx & "]C)-LEN("": ""))"

Cells(x, k + 2).Value = (Split(Split(Cells(x - 1, 2 + k), "Quantity : ")(1), " ")(0)) * 1

I seldom use split function, but after I saw your code will surely delve into it more... thanks...
 
Upvote 0
If interested, you could also do it without the second Split
VBA Code:
getgrams = Val(Split(sStr, "Grams : ")(1))
 
Upvote 0
If interested, you could also do it without the second Split
VBA Code:
getgrams = Val(Split(sStr, "Grams : ")(1))
thanks, I'll try to incorporate it later. loop is really the culprit of sluggish code for MSExcel (VBA). will update my code later... thanks again..
 
Upvote 0
@NoSparks @Peter_SSs
down with a flu! thankfully not covid! :)
it was great help in its runtime aspect, from 86secs. down to 20secs both code run smoothly with a difference of some micro seconds to spare but thanks to both of you
 
Upvote 0
Glad we were able to help. Thanks for letting us know. :)
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,525
Members
449,088
Latest member
RandomExceller01

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