Apologies if this has already been asked and answered but I couldn't find a satisfactory answer.
I have a list of chemical formulas including, in this order: C, H, N and O. And I would like to pull the number after each of these letters. The problem is that not all the formulas contain an N. All contain a C, H and O however. And the number can be either single, double or (in the case of H only) triple digit.
Thus the data looks like this:
- C20H37N1O5
- C10H12O3
- C20H19N3O4
- C23H40O3
- C9H13N1O3
- C14H26O4
- C58H100N2O9
I'd like each element number for the list in separate columns. So in the first example it would be:
20 37 1 5
I've been trying:
=IFERROR(MID(LEFT(A2,FIND("H",A2)-1),FIND("C",A2)+1,LEN(A2)),"")
to separate out the C#. However, after this I get stuck as the H# is flanked by either an O or N.
Is there an excel formula or VBA that can do this?
I did this in VBA, using regular expressions. You can probably do it like Vityata suggests by looping through the string too, but I suspect that this is slightly faster and easier to read.
Then you use the formula in your sheet like normal:
Column C contains the number of carbon atoms, column D the number of nitrogen atoms. Just expand on this by copying the formula and changing the element it searches for.
Use Regular Expressions
This is a good task for regular expressions (regex). Because VBA doesn't support regular expressions out of the box we need to reference a Windows library first.
Add reference to regex under Tools then References
and selecting Microsoft VBScript Regular Expression 5.5
Add this function to a module
Use the function like this in a cell formula
E.g. in cell B2:
=ChemRegex($A2,B$1)
and copy it to the other cellsRecognize also chemical formulas with multiple occurrences of elements like
CH3OH
orCH2COOH
Note that the code above cannot count something like
CH3OH
where elements occur more than once. Then only the firstH3
is count the last is omitted.If you need also to recognize formulas in the format like
CH3OH
orCH2COOH
(and summarize the occurrences of the elements) then you need to change the code to recognize these too …Recognize also chemical formulas with 2 letter elements like
NaOH
orCaCl2
In addition to the change above for multiple occurrences of elements use this pattern:
CaCl2
works but notcacl2
orCACL2
.Note that this doesn't proof if these letter combinations are existing elements of the periodic table. So this will also recognize eg.
Xx2Zz5Q
as fictive elementsXx = 2
,Zz = 5
andQ = 1
.To accept only combinations that exist in the periodic table use the following pattern:
With VBA this is an easy task - you have to loop through the chars and check the values for being numeric. With Excel, the solution includes some redundancy. But it is doable. E.g.,
C20H37NO5 will return 20375 if you apply the following formula:
Currently, it checks the first 9 characters for being numeric. If you want to include more than 9, then simply add a few lines to the formula.
There is a small trick in the formula - the
1*
. It converts a text character to a numeric, if it is possible. Thus, a5
as a text, multiplied by1
becomes a numeric character.If you want a vba solution to extract all numbers my preferred solution is to use Regular Expressions. The following code will extract all numbers from a string
This seems to work just fine:
Formula in
B2
is below. Drag across and downOr a shorter array formula, which must be entered with ctrl+shift+enter
If you wanted to keep the VBA super simple, something like this works as well:
Use it like so:
Use split and like method.