Please see Creating Excel Add-ins: Molecular Weight Example (Update #1) for updates, new information, and leaving new comments.
We often use the same functions over and over again, such as sine and cosine, so they’re programmed into everything from calculators to computer programming languages to spreadsheets. In Excel, you can write your own functions. In this thread, I’ll give a quick "How To" for Excel 2007/2010 and provide an example add-in which calculates molecular mass.
EDIT 1: If anything would benefit from a clarification or edit, please let me know by commenting below!
EDIT 2: Added support for isotopes at the end of this post. The code in this first post is the most up-to-date and using the code in the discussion following this first post is not recommended.
What’s a function?
In Excel, a function can be called to perform a calculation or task. For example, the sine function sin(x) returns the sine of its single argument x.
The sine function used in Excel. Note that Excel calculates 1.2+2^2=5.2 before passing 5.2 to the sine function.
Enabling Excel’s ‘Developer’ Tab
The Developer tab, which is hidden by default, is necessary to program stuff into Excel. If yours is already showing, you can skip ahead.
For Excel 2007: http://www.asap-utilities.com/blog/inde ... oper-mode/
For Excel 2010: http://www.excel-2010.com/the-developer ... xcel-2010/
For Excel 2011: http://www.gilsmethod.com/how-to-enable ... excel-2011
Writing your First Function
Once the Developer tab is enabled, select it and then select the "Visual Basic" button. The Visual Basic editor should pop up.
Next, create a new module. In the "Project - VBAProject" panel, right-click "Microsoft Excel Objects", and select Insert -> Module.
Then insert the code:
- Code: Select all
Function MyAverage(firstNumber As Double, secondNumber As Double)
MyAverage = (firstNumber + secondNumber) / 2
End Function
Now that it’s written into a module, it’ll work for the current spreadsheet that you have open. If you save this spreadsheet, it’ll work for anyone that opens it and enables macros. (Note that it’ll have to be saved as an *.xlsm file instead of as an *.xlsx file if you want the code to work.)
In any cell, you can now use your new function as MyAverage(x1, x2).
- Code: Select all
=MyAverage(1,2)
A More Complex Function
Chemists often have to find the molecular mass (or molecular weight) of a chemical species. This is done by adding up all of the weights of the component atoms of the species. For example, water (H2O) has two hydrogen atoms and one oxygen atom, so you can pick up a periodic table, find the mass for hydrogen, double it, and add the mass of oxygen, and that’s your molecular mass. This can become tedious, particularly for more complex chemicals, i.e., [P(C4H9)3C14H29]Cl.
A function mw(x) can be created to compute the molecular mass (aka molecular weight) automatically. This is a more complex function, and its code is attached below.Obsolete: Please see Creating Excel Add-ins: Molecular Weight Example (Update #1) for newer code with a bug fix, newer values, and new features.
To use this code, directly copy/paste this code into a module. The function mw() should now work for that spreadsheet. Note that mw(H2) gives the molecular mass of whatever the cell H2 has in it, not the molecular mass of H2. To directly enter a molecule’s formula, put quotes around it - i.e., mw("H2").
As for syntax, it’s
- Code: Select all
=mw("[P(C4H9)3C14H29]Cl")
Note that the function considers ( and [ to be the same thing, and likewise for ) and ]. Also, there's no practical limit to the number of ()'s you can use.
Making the Code into an Add-in
The functions that you’ve just written work for only the spreadsheet that you’ve put them into. If you save it (as an *.xlsm file) and send it to someone, they’ll be able to use those functions in that spreadsheet too.
However, if you want the functions to be automatically available to you in all of your spreadsheets, you can save them as add-ins. Functions in your add-ins will work for all spreadsheets that you open up on your computer until you disable or delete the add-ins.
To save the current code as an add-in, simply go to Save As and, under file type, select "Excel Add-In (*.xlam)". The add-in should be saved in specific folder which Excel will go to by default.
Once the add-in is saved, it still needs to be enabled. Enabling the add-in:
Excel 2007: http://peltiertech.com/WordPress/instal ... xcel-2007/
Excel 2010: http://www.dq.winsila.com/tips-tricks/m ... -2010.html
Once enabled, the functions will work on all of your spreadsheets!
Note
For those who just wanted to get an introduction to programming Excel add-ins, you can stop here. The following section relates to adding in functionality for isotopes. No additional programming skills will be gained; it's just adding code like before and explaining how to use it.
Isotope Variation
The code just installed will give you the prevalence-weighted molecular weight, just like what you find on a periodic table. However some users will want the values of specific isotopes. For this, just go to the Visual Basic button in Excel’s Developer tab again, make a new module (as you did before), and copy paste in the code in this file:Obsolete: Please see Creating Excel Add-ins: Molecular Weight Example (Update #1) for newer code with a bug fix, newer values, and new features.
Now you have a new function, mwi(chemicalSpecies, useMostCommonIsotopes). This function has more in it (as you can tell from the significantly larger file size), providing isotope data. Use it just like mw(chemicalSpecies) as we did before, EXCEPT:
1. You can use an exclamation point and then a number before an element symbol to specify a specific isotope.
Example: =mwi("!1H") gives the molecular weight for 1H.
2. You can use a "true" argument in mwi() to have all element symbols which haven’t had their isotope specified default to their most common isotope. (Note: Putting "FALSE" in is purely optional since it’ll default to FALSE if you don’t put anything.)
EX: mwi("H", TRUE) gives the molecular weight for 1H, since 1H is the most common isotope of hydrogen.
EX: mwi("H") and mwi("H", FALSE) give the prevalence-weighted molecular weight of hydrogen, just like mw("H").
Other examples:
EX: mwi("!2H2O", TRUE) and mwi("!2H2!16O") would both give the molecular weight for 2H216O.
EX: mwi("!2H2O") and mwi("!2H2O", FALSE) would both give the molecular weight for regular heavy water, 2H2avO.
EX: mwi("!1H!2HO") would give the molecular weight for 1H 2H avO.
The examples use very simple formulas for the sake of simple demonstration, but the code can easily handle far more complex chemicals. In general, specify the isotopes you want, then "TRUE" if you want the rest to be the most common isotopes for their species or "FALSE" if you want the rest to be prevalence-weighted.
Licensing
As the original author, I hereby release code attached in this post to the public domain. I would ask, though it is optional, that a link back to this thread be provided. No further attribution is required.
Acknowledgements
NIST for providing the isotopic weights used.
Microsoft for providing the Office platform.
Providers for the linked resources in the guide above.
The SPCF for hosting this thread.
Members commenting below for their contributions, comments, and feedback.
Bugs and Errata
2014 03 05 - chebob512 has noted that all versions of the add-in through the current date incorrectly add the digits for uncertainty to the end of the digits for atomic weights. In most cases this results in a small-ish error, e.g. for Hydrogen the atomic weight 1.007947 was used instead of the actual 1.00794, but it will be corrected in an upcoming version.
2013 05 13 - It was noted that the second and third isotopes of hydrogen, 2H and 3H, were not recognized when the user referred to them as "!2H" or "!3H". This is because they were referred to as "!2D" or "!3T", since those isotopes are usually named. Versions 3.0 and later will now accept "!2H" and "!3H" as valid names for these isotopes. "!2D" and "!3T" still work too.