Help with UDF syntax and parsing

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,532
Office Version
  1. 365
Platform
  1. Windows
I want to write a little function to compare the amount of sodium in several products. The data is currently in a text string of the form: Xg Ymg, where X is one serving of the product and Y is the amount of sodium in that serving. I need to extract the X and Y then calculate Y/X. The text string might also be blank. Here's some sample data:

Book1
CD
428.35g 0mg0.00
528.35g 0mg0.00
628g 135mg4.82
7
828g 135mg4.82
928g 135mg4.82
1028g 65mg2.32
11
1228g 110mg3.93
Sheet1
Cell Formulas
RangeFormula
D4:D5D4=0/28.35
D6,D8:D9D6=135/28
D10D10=65/28
D12D12=110/28


Is there a simple way to extract those values in VBA?

Is there a simple Excel expression that will extract the two numerical values and pass them to the UDF? I suppose I'd have to make the parameters optional in order to be able to handle the blank case.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
This will work if the units always going to be g and mg.

Excel Formula:
=LET(s,SEARCH(" ",A1),r,RIGHT(A1,LEN(A1)-s),LEFT(r,LEN(r)-2)/LEFT(A1,s-2))
 
Upvote 0
This will work if the units always going to be g and mg.

Excel Formula:
=LET(s,SEARCH(" ",A1),r,RIGHT(A1,LEN(A1)-s),LEFT(r,LEN(r)-2)/LEFT(A1,s-2))
Wow! It works. I'll have to study it to understand how. It does get a value error on the blank rows, but that's not a serious problem, so I've marked this as solved.

Thank you

Book1
CDE
428.35g 0mg0.000.00
528.35g 0mg0.000.00
628g 135mg4.824.82
7#VALUE!
828g 135mg4.824.82
928g 135mg4.824.82
1028g 65mg2.322.32
11#VALUE!
1228g 110mg3.933.93
Sheet1
Cell Formulas
RangeFormula
D4:D5D4=0/28.35
D6,D8:D9D6=135/28
D10D10=65/28
E4:E12E4=LET(s,SEARCH(" ",C4),r,RIGHT(C4,LEN(C4)-s),LEFT(r,LEN(r)-2)/LEFT(C4,s-2))
D12D12=110/28
 
Upvote 0
Glad it did work :) You can overcome the error problem simply with an IFERROR check:
Excel Formula:
=IFERROR(LET(s,SEARCH(" ",A1),r,RIGHT(A1,LEN(A1)-s),LEFT(r,LEN(r)-2)/LEFT(A1,s-2)),"")
First, the formula finds the position space and takes the right text until the space position by substracting it form the text length. Then, trims the last two characters "mg" from the end.
Second, it finds the position of space again but this time takes the left text until the space. And trims two characters again "g " form the end.
Then divides them.
 
Upvote 0
Solution
Glad it did work :) You can overcome the error problem simply with an IFERROR check:
Excel Formula:
=IFERROR(LET(s,SEARCH(" ",A1),r,RIGHT(A1,LEN(A1)-s),LEFT(r,LEN(r)-2)/LEFT(A1,s-2)),"")
First, the formula finds the position space and takes the right text until the space position by substracting it form the text length. Then, trims the last two characters "mg" from the end.
Second, it finds the position of space again but this time takes the left text until the space. And trims two characters again "g " form the end.
Then divides them.
Even better and I think I can actually follow the logic!

I moved the solution to this message.

Thank you very much! 👍👏🥰
 
Upvote 0
with Excel 365, try the following:

Let_a.xlsm
ABCD
1Excel
2365
328.35g 0mg000
428g 135mg4.8214285714.8214294.821429
528g 65mg2.3214285712.3214292.321429
628g 110mg3.9285714293.9285713.928571
4f
Cell Formulas
RangeFormula
B3B3=0/28.35
C3:C6C3=IFERROR(LET(s,SEARCH(" ",A3),r,RIGHT(A3,LEN(A3)-s),LEFT(r,LEN(r)-2)/LEFT(A3,s-2)),"")
D3:D6D3=IFERROR(TEXTBEFORE(TEXTAFTER(A3,"g"),"m")/TEXTBEFORE(A3,"g"),"")
B4B4=135/28
B5B5=65/28
B6B6=110/28
 
Upvote 0
with Excel 365, try the following:

Let_a.xlsm
ABCD
1Excel
2365
328.35g 0mg000
428g 135mg4.8214285714.8214294.821429
528g 65mg2.3214285712.3214292.321429
628g 110mg3.9285714293.9285713.928571
4f
Cell Formulas
RangeFormula
B3B3=0/28.35
C3:C6C3=IFERROR(LET(s,SEARCH(" ",A3),r,RIGHT(A3,LEN(A3)-s),LEFT(r,LEN(r)-2)/LEFT(A3,s-2)),"")
D3:D6D3=IFERROR(TEXTBEFORE(TEXTAFTER(A3,"g"),"m")/TEXTBEFORE(A3,"g"),"")
B4B4=135/28
B5B5=65/28
B6B6=110/28
Impressive. That is simpler and cleaner. I think I need to get a book on new features in Excel 365.

Thanks for adding that. 👍👍
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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