Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to do math on cells with letters

4 views
Skip to first unread message

ZenMasta

unread,
Dec 27, 2009, 7:01:39 PM12/27/09
to
Hi,
I'm trying to figure out portion info for food nutrition.

I have a sheet that has nutrition facts etc. However the columns will often
have letters like g, mg, mcg (grams miligrams, micrograms etc)

b12=33g d3=3

Well, =b12/d3 = 0 because the letter is throwing it off.
Is there a way I can ignore letters in a field so I can still perform math
on those fields?

Joe User

unread,
Dec 27, 2009, 7:47:45 PM12/27/09
to
"ZenMasta" <m...@nospamkthx.bai> wrote:
> b12=33g d3=3
> Well, =b12/d3 = 0 because the letter is
> throwing it off. Is there a way I can ignore letters in a
> field so I can still perform math on those fields?

Yes. But it would be better (less problematic) if you do not put the text
in the cell in the first place.

Two alternatives:

1. Simply put the text in an adjacent cell.

2. Custom format the cell with the number, e.g. 0 "g".


----- original message -----

"ZenMasta" <m...@nospamkthx.bai> wrote in message
news:ObyPvD1h...@TK2MSFTNGP02.phx.gbl...

Max

unread,
Dec 27, 2009, 7:46:06 PM12/27/09
to
One thought, remove the letter using SUBSTITUTE
then do/force a calculation on the resulting text number
=SUBSTITUTE(B12,"g","")/D3

"ZenMasta" <m...@nospamkthx.bai> wrote in message
news:ObyPvD1h...@TK2MSFTNGP02.phx.gbl...

Rick Rothstein

unread,
Dec 27, 2009, 11:09:53 PM12/27/09
to
Give this a try...

=LOOKUP(9.9E+307,--LEFT(B12,ROW($1:$99)))/D3

--
Rick (MVP - Excel)


"ZenMasta" <m...@nospamkthx.bai> wrote in message
news:ObyPvD1h...@TK2MSFTNGP02.phx.gbl...

ZenMasta

unread,
Dec 27, 2009, 11:54:39 PM12/27/09
to
Max, I tried your suggestion and it gave me an Err:508


Joe,
I'm just the recipient of this data I didn't design it, yeah I agree it'd
have been better if they could have separated the unit of measurement from
the value.
In regards to formatting the cell... the cells already have these values in
them so if I create a user defined format code like you suggest it wouldn't
make any difference.

Rick, I tried your solution also and got Err:508

Rick Rothstein

unread,
Dec 28, 2009, 12:31:51 AM12/28/09
to
Err:508? Where are you putting the formula... in a worksheet cell or VB
code? If a worksheet cell, where are you seeing this error number at?

--
Rick (MVP - Excel)


"ZenMasta" <m...@nospamkthx.bai> wrote in message

news:eTCOdn3h...@TK2MSFTNGP05.phx.gbl...

ZenMasta

unread,
Dec 28, 2009, 1:30:53 AM12/28/09
to
I'm putting the formula in a cell. the error appears in the cell.

"Rick Rothstein" <rick.new...@NO.SPAMverizon.net> wrote in message
news:#Td0E83h...@TK2MSFTNGP05.phx.gbl...

joeu2004

unread,
Dec 28, 2009, 3:08:19 AM12/28/09
to
"ZenMasta" <m...@nospamkthx.bai> wrote:
> I'm just the recipient of this data I didn't design it

Okay. Perhaps we could help you better by suggesting a macro to
convert the data. But we would need more information.

Returning to your original question....


You wrote previously:


> the columns will often have letters like g, mg, mcg

[....]


> b12=33g d3=3
>
> Well, =b12/d3 = 0 because the letter is throwing it off.
> Is there a way I can ignore letters in a field so I can
> still perform math on those fields?

Try the following.

=LEFT(B12,MIN(FIND({"g","m"},B12&"gm")-1) / D3

The ``&"gm"`` is needed only if there might be no text to the right of
the number.

Add more letters to the first FIND argument as needed. The following
demonstrates the most flexible:

=LEFT(B12,
MIN(FIND(
{"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"},
B12&"abcdefghijklmnopqrstuvwxyz")-1) / D3


----- original message ------


----- previous message -----

Bernd P

unread,
Dec 28, 2009, 3:53:55 AM12/28/09
to
Hello,

I suggest not to use Excel for this but www.wolframalpha.com:

Enter, for example (maybe not reasonable, just to show that you can
use plenty of different measures):
20g butter + 1 slice of bread + 10000mg salmon

You will get nutrition facts you (I bet) will never get with any self-
made Excel application.

Regards,
Bernd

Max

unread,
Dec 28, 2009, 9:43:54 AM12/28/09
to
ZenMasta,

> Max, I tried your suggestion and it gave me an Err:508

Don't know how the above happened,
but here's living proof of the formula working as advertised:
http://cjoint.com/?mCpN5ufqHO

--
Max
Singapore


Rick Rothstein

unread,
Dec 28, 2009, 9:56:36 AM12/28/09
to
I'm not sure what to tell you... I have never seen Err:508 appear in a cell
(unless those characters were deliberately placed in the cell as the result
of a formula or VB code result). Assuming you are using a PC version of
Excel, if you send me a copy of your worksheet (remove the NO.SPAM stuff
from my email address if you do), I'll be happy to take a look at it and see
if I can figure out what is going on.

Just in case you want to continue trying to fix the problem on your own,
this formula...

=LOOKUP(9.9E+307,--LEFT(A1,ROW($1:$99)))

will return the leading number at the beginning of a value in A1 even if
there is text after that number.

--
Rick (MVP - Excel)


"ZenMasta" <m...@nospamkthx.bai> wrote in message

news:uWUgPd4h...@TK2MSFTNGP06.phx.gbl...

Joe User

unread,
Dec 28, 2009, 10:45:01 AM12/28/09
to
"ZenMasta" <m...@nospamkthx.bai> wrote:
> I'm just the recipient of this data I didn't design it

Okay.

Perhaps we could help you better by suggesting a macro to convert the data.
But we would need more information.

Returning to your original question....


You wrote previously:


> the columns will often have letters like g, mg, mcg

[....]


> b12=33g d3=3
>
> Well, =b12/d3 = 0 because the letter is throwing it off.
> Is there a way I can ignore letters in a field so I can
> still perform math on those fields?

Try the following.

=LEFT(B12,MIN(FIND({"g","m"},B12&"gm")-1) / D3

The ``&"gm"`` is needed only if there might be no text to the right of the
number.

Add more letters to the first FIND argument as needed. The following
demonstrates the most flexible:

=LEFT(B12,
MIN(FIND(
{"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"},
B12&"abcdefghijklmnopqrstuvwxyz")-1) / D3


----- original message ------

"ZenMasta" <m...@nospamkthx.bai> wrote in message

news:eTCOdn3h...@TK2MSFTNGP05.phx.gbl...


----- previous message -----

Joe User

unread,
Dec 28, 2009, 11:06:00 AM12/28/09
to
Sorry ... just testing a theory.

Joe User

unread,
Dec 28, 2009, 11:05:26 AM12/28/09
to

Joe User

unread,
Dec 28, 2009, 11:08:18 AM12/28/09
to

Joe User

unread,
Dec 28, 2009, 11:20:16 AM12/28/09
to
Sorry .... one final test (sigh)

ZenMasta

unread,
Dec 28, 2009, 5:37:43 PM12/28/09
to
Rick, your original solution worked. I was using calc and google docs. But I
just tried it with excel at work and it is fine.

Normally this type of stuff has always worked on other spreadsheet apps.
Just not this one.

Thank again.


"Rick Rothstein" <rick.new...@NO.SPAMverizon.net> wrote in message

news:uRV4o38h...@TK2MSFTNGP02.phx.gbl...

ZenMasta

unread,
Dec 28, 2009, 5:44:24 PM12/28/09
to
Thanks for the suggestion. I've used WA before but I already have an excel
sheet that lists the nutrion facts for certain items and there are
several... too many to want to type manually into their engine.


"Bernd P" <bplu...@gmail.com> wrote in message
news:4b533bbc-73b1-4944...@a6g2000yqm.googlegroups.com...

ZenMasta

unread,
Dec 28, 2009, 11:50:12 PM12/28/09
to
Hey Max I just double checked... your first reply had commas instead of semi
colons by mistake so yeah works for sure with semi colons.

"Max" <demec...@yahoo.com> wrote in message
news:OrDyww8h...@TK2MSFTNGP05.phx.gbl...

Max

unread,
Dec 29, 2009, 2:34:57 AM12/29/09
to
There was no mistake. What I plugged into the working sample is exactly the
same expression in my 1st response. It's probably the regional settings
thingy - I use commas as delimiters (same as US, UK) whilst you use
semicolons (Continental Europe?). Fortunately, it seems that Excel knows how
to show it properly depending on the settings used.
--
Max
Singapore
----

"ZenMasta" <m...@nospamkthx.bai> wrote in message
news:erSFoJEi...@TK2MSFTNGP06.phx.gbl...

ZenMasta

unread,
Dec 30, 2009, 12:16:37 PM12/30/09
to
I'm in the US too. Strange. But glad it worked. Thanks again.

"Max" <demec...@yahoo.com> wrote in message

news:Oz1oulFi...@TK2MSFTNGP02.phx.gbl...

SanCarlosCyclist

unread,
Dec 31, 2009, 1:55:14 PM12/31/09
to
Max, is there a way to get this to work if there is any letter in a
cell? Example:
121c
133bb
hh212

Is the a way to use a single substitute that would use only the number
is each of these cells?

Max

unread,
Dec 31, 2009, 3:40:56 PM12/31/09
to
For this
> 121c
> 133bb

Rick's offer: =LOOKUP(9.9E+307,--LEFT(A1,ROW($1:$99)))
will strip it as real numbers

To handle it both ways, eg:
> hh212
> 133bb

This works, taken from a posting by macropod:

"..Here's a more flexible formula to strip out all leading and trailing
non-numerics from a cell:
=MID(A1,MIN(IF(ISNUMBER((--MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))),(ROW(INDIRECT("1:"&LEN(A1)))),"")),MAX(IF(ISNUMBER((--MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))),(ROW(INDIRECT("1:"&LEN(A1)))),""))-MIN(IF(ISNUMBER((--MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))),(ROW(INDIRECT("1:"&LEN(A1)))),""))+1)
This is an array formula, so you input it with Ctrl-Shift-Enter, after which
it will ben enclosed in a pair of braces (ie '{}') .."

--
Max
Singapore
----
"SanCarlosCyclist" <sancarlo...@gmail.com> wrote in message
news:b2290351-7de0-4f3e...@j14g2000yqm.googlegroups.com...


> Max, is there a way to get this to work if there is any letter in a
> cell? Example:
> 121c
> 133bb
> hh212
>

> Is there a way to use a single substitute that would use only the number

Rick Rothstein

unread,
Dec 31, 2009, 4:59:31 PM12/31/09
to
> To handle it both ways, eg:
>> hh212
>> 133bb
>
> This works, taken from a posting by macropod:
>
> "..Here's a more flexible formula to strip out all leading and trailing
> non-numerics from a cell:
> =MID(A1,MIN(IF(ISNUMBER((--MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))),(ROW(INDIRECT("1:"&LEN(A1)))),"")),MAX(IF(ISNUMBER((--MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))),(ROW(INDIRECT("1:"&LEN(A1)))),""))-MIN(IF(ISNUMBER((--MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))),(ROW(INDIRECT("1:"&LEN(A1)))),""))+1)
> This is an array formula, so you input it with Ctrl-Shift-Enter, after
> which it will ben enclosed in a pair of braces (ie '{}') .."

I believe this much shorter formula will return the (first) number in the
text no matter where it is located within the text...

=LOOKUP(9.9E+307,--LEFT(MID(A1,--LEFT(MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))),LEN(A1)),ROW($1:$99)))

ebloch

unread,
Dec 31, 2009, 5:01:10 PM12/31/09
to
free ASAP Utilities has a function under "Text" to remove all text
characters in a cell or range.

Eric

"ZenMasta" <m...@nospamkthx.bai> wrote in message
news:ObyPvD1h...@TK2MSFTNGP02.phx.gbl...

Max

unread,
Dec 31, 2009, 5:46:20 PM12/31/09
to
Rick, that's great. How can your formula be modified so that it returns it
as text numbers instead, with all leading zeros retained?

Bernd P

unread,
Jan 1, 2010, 1:06:10 PM1/1/10
to
Hello,

Why not just
=--RegExpReplace(A1,"^\D*(\d+).*$","$1")
?

This UDF you can find at
http://sulprobil.com/html/regexp.html

Regards,
Bernd

Rick Rothstein

unread,
Jan 1, 2010, 2:37:11 PM1/1/10
to
Why? Well, from my personal standpoint, I would respond that UDF's tend to
be slower than formulas and that particular UDF requires you to learn
Regular Expression syntax if you do not already know how to construct them.
I will concede, though, that your method can more easily respond to the
question that Max asked (how do you make the return value a string so
leading zeroes can be preserved?)... simply removing the double minus sign
from the front of your formula will do that... my formula appears not to be
so easy to modify for this functionality.

--
Rick (MVP - Excel)

"Bernd P" <bplu...@gmail.com> wrote in message

news:de5c4581-755b-4c20...@26g2000yqo.googlegroups.com...

Roger Govier

unread,
Jan 1, 2010, 6:31:41 PM1/1/10
to
Hi

If you have control of the sheet, then you could format the cells as
Format>Cells>Number>Custom>General "g" or General "mg" etc.

Entering 33 in cell B12 would display as 33 g, but all that would be held in
the cell would be 33 so it can be used in further calculation.

In place of general, you could use #.00 for example, if you wanted to
display 33.00 g

--
Regards
Roger Govier

"ZenMasta" <m...@nospamkthx.bai> wrote in message
news:ObyPvD1h...@TK2MSFTNGP02.phx.gbl...
> Hi,
> I'm trying to figure out portion info for food nutrition.
>
> I have a sheet that has nutrition facts etc. However the columns will
> often have letters like g, mg, mcg (grams miligrams, micrograms etc)
>
> b12=33g d3=3
>
> Well, =b12/d3 = 0 because the letter is throwing it off.
> Is there a way I can ignore letters in a field so I can still perform math
> on those fields?
>

> __________ Information from ESET Smart Security, version of virus
> signature database 4736 (20100101) __________
>
> The message was checked by ESET Smart Security.
>
> http://www.eset.com
>
>
>

__________ Information from ESET Smart Security, version of virus signature database 4736 (20100101) __________

The message was checked by ESET Smart Security.

http://www.eset.com

Bernd P

unread,
Jan 2, 2010, 6:38:34 AM1/2/10
to
On 31 Dez. 2009, 22:46, "Max" <demecha...@yahoo.com> wrote:
> Rick, that's great. How can your formula be modified so that it returns it
> as text numbers instead, with all leading zeros retained?
Hello Max,

=RegExpReplace(A1,"^\D*(\d+).*$","$1")

Just press ALT + F11, enter a new module, copy the macro text from
above link into the new module and go back to your worksheet and use
the command.

Regards,
Bernd

Max

unread,
Jan 2, 2010, 9:00:24 PM1/2/10
to
Bernd, thanks for responding. Its more for the OP, SanCarlosCyclist to
consider and take up whichever option s/he finds suitable. Hopefully s/he
would be courteous to post a reply here, given the fortune of receiving so
many useful responses to the query.

0 new messages