Floating Point Errors in Excel VBA

I recently had a project where I had to total dollar figures in two columns and compare that total to the reported total in a third. Easy, right? The VBA code was pretty simple: If Cells(ActiveCell.Row, 4).Value + Cells(ActiveCell.Row, 6).Value <> _      Cells(ActiveCell.Row, 8).Value Then         Cells(ActiveCell.Row, 10).Value = “Totals don’t match”         ‘(e.g. if D2 + F2 <> H2 Then J2 contains an error message) End If In a set of 70 rows, I kept getting two rows where the totals were equal, but reported as not equal. I tested the … Continue reading

Dates, Macs and PCs

I had a question from a friend about dates today. She had 2 spreadsheets, both showing 1/1/2017 as a date. When she copied one to another, the dates were all wrong–they were off by 4 or more years. Here is an example of what her problem was: The problem is that 1900 is not a leap year, according to the standard rules: A normal year is defined as 365 days. (Plus a quarter day which is resolved by having a leap year and adding a day (Feb 29) every four years.) A year is a leap year if: it is divisible by 4. It is not … Continue reading

The RAND Function

In a class, someone asked about a random number generator. I replied with information about previous iterations of the RAND() function built into Excel. In researching it, I realized that Excel has moved on. Hence this post. =RAND() in a cell will display a random number between 0 and 1. The function is volatile, meaning that each time you press Enter to edit a cell or press F9 to recalculate the workbook, the RAND() function displays a different random number. If you want the number between 0 and 100, use the formula: =RAND()*100. This will display the number, but it … Continue reading

Fix Gene Names Turned into Dates Problem

I became aware of this issue in http://www.sciencemag.org/news/sifter/one-five-genetics-papers-contains-errors-thanks-microsoft-excel?utm_source=newsfromscience&utm_medium=facebook-text&utm_campaign=excel-6929. The following user-defined Function is a start. You can add to the Case statement to add whatever gene names you like, with the caveat that once Excel finds a match, it will jump out of the Case statement. Add this to your Personal Macro Workbook and save it. You can then find the function in the User Defined Functions category of the Function Wizard. Add the function to a separate column, reference the first cell with the problem, copy it down the column, then Copy | Paste | Values into the same column to … Continue reading

Hiding Data on an Excel Spreadsheet

Suppose you need to hide a small section of data in Excel, but you still want to calculate with it. Highlight the cells, right-click, and select Format Cells. Click the Number tab, and select the Custom category on the left. Type three semicolons (;;;) in the Type: field, and click OK. The numbers are invisible, but you can still use them. … Continue reading

Save an Excel Chart as a Template

To save a chart as a template: Create your chart, and then right click it. Select Save as Template… You’ll save a file with a CRTX extension in your default Microsoft Excel Templates folder. To create a new chart based on the template: Select the data you want to chart. Click the Insert tab / Recommended Charts, the All Charts tab, and the Templates folder. In the My Templates box, pick the one to apply, then click OK. Some elements, like the actual text in the legends and titles, won’t translate unless they’re part of the data selected. However, all … Continue reading

Using Range.Offset in Excel VBA

To select a cell in Excel, you have two basic methods: RANGE and CELLS: Range (“A1”).Select Range(“RangeName”).Select Cells(3, 4).Select ‘Selects Row 3, Column 4, i.e. cell D3 Range works well for hard-coded cells. Cells works best with calculated cells, especially when you couple it  with a loop: For i = 1 to 10      Cells(i, 1).value = i ‘ fill A1 through A10 with the value of i Next i Note that your focus does not change. Whatever cell you were in when you entered the loop is where you are when you leave the loop. This is way faster … Continue reading

Mail Merge Problem: Leading Zeroes Missing from Zip Codes

When you use and Excel spreadsheet as a data file in a Word mail merge, formatting zip codes can sometimes make you want to tear all the hair out of your head. This is a particularly annoying problem because most users assume they are solving the problem by correctly formatting zip codes in Excel.  However, correctly formatted zip codes in Excel sometimes still arrive in Word without their leading zeroes. Here is one way to fix the problem permanently: Start Word, and then open a new blank document. Go to Word Options In Word 2007, click the Office Button, and … Continue reading