by Allen Wyatt
(last updated November 30, 2019)
Ruby is trying to find an easy way to determine the number of atoms in molecular formulas of some chemical structures. For instance, a cell might contain a formula such as C12H10N6F2. In this case the number of atoms is 12 + 10 + 6 + 2 = 30. Ruby has about 300 of these formulas to do and was wondering if there is an Excel formula that can be used to do this.
First, the bad news: There is no easy way to do this.
There; with that out of the way, we can start to look for solutions. The example chemical formula provided by Ruby may lead some to think that counting atoms is a simple process of substituting the alphabetic characters with something else so that just the numeric characters can be evaluated. As an example, here is Ruby's example chemical formula:
If you replace the alphabetic characters with plus signs, you get this:
Looks like a simple formula now, right? This is deceiving, because while it will work in this instance, it may not work at all for Ruby's other chemical formulas. Consider the following chemical formula that many people will be familiar with:
Doing the same substitution renders this:
Problem is, there is an implied count of 1 whenever there is a single element—for example, the oxygen element. Thus, H20 is actually 3 atoms.
So now we can come up with a way to simply account for the implied 1, right? Sure; this can be done. It can be done most easily and cleanly with a macro, such as the following user-defined function:
Function CountAtoms(ChemForm As String) Dim sNewNum As String Dim sTemp As String Dim iNewAtoms As Integer Dim iTotalAtoms As Integer Dim J As Integer sNewNum = "" iTotalAtoms = 0 For J = 2 To Len(ChemForm) sTemp = Mid(ChemForm, J, 1) If sTemp >= "0" And sTemp <= "9" Then sNewNum = sNewNum & sTemp ElseIf sTemp <= "Z" Then iNewAtoms = Val(sNewNum) If iNewAtoms = 0 Then iNewAtoms = 1 iTotalAtoms = iTotalAtoms + iNewAtoms sNewNum = "" End If Next J iNewAtoms = Val(sNewNum) If iNewAtoms = 0 Then iNewAtoms = 1 iTotalAtoms = iTotalAtoms + iNewAtoms CountAtoms = iTotalAtoms End Function
In order to use this function in your worksheet, you would simply reference the chemical formula:
If the chemical formula is in cell A1, this function returns the count you desire. It will even work with formulas such as the following:
Note that these rely on two-character element names, of which there are many. It does require, however, that the second character of a two-character element name not be capitalized.
So, will this approach work with all chemical formulas? Not really; it only works with the simple ones we've covered so far. You see, chemical formulas can get quite complex. Consider the following example:
When an initial number appears like this, then the formula is to be multiplied by that value. Thus, instead of the normal 3 atoms in H2O, this formula would have 6 atoms.
It gets worse. Consider the following valid chemical formulas:
Note the parentheses followed by a number. In this nomenclature, the value immediately following the closing parenthesis indicate how many of the molecules within the parentheses are in the larger molecule. Thus, in the second example there are 3 molecules of SO4 and 18 molecules of H2O in the overall molecule. This obviously affects the number of atoms in the entire formula. To compound complexity, parentheses can even be nested:
This can still be addressed with a more complex macro. Rather than reinvent the wheel here, though, if you are working with complex chemical formulas such as these, you might want to consider using the macros provided at this site:
Note that the macros aren't implemented as user-defined functions. To use them you simply select the cells with the formulas, run the macro, and then the macros modify information in the columns to the right of the selected chemical formulas. Full instructions are included with the code at the above website.
You'll also need to make sure you enable, in the Visual Basic Editor, regular expressions. You do this by choosing Tools | References and then scrolling through the available references to locate the Microsoft VBScript Regular Expressions 5.5 option. Make sure the check box to the left of the reference is selected, then click OK.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13707) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
On your system you may have workbooks that contain macros you know are safe to use. Microsoft provides two things you can ...Discover More
Sometimes, when you upgrade to a new version of Excel, you could run into a problem recording macros that you had no ...Discover More
The Text-to-Columns tool is an extremely powerful feature that allows you to divide data in a variety of ways. Excel even ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.