Your Excel Spreadsheet Smells

Do your spreadsheets smell? This week, a tweet from Felienne Hermans caught my eye.

  • “Our @icse2012 paper on spreadsheet smells already has a citation before publication”

Spreadsheet smells? I’ve seen some stinky spreadsheets, but have never read a conference paper on spreadsheet smells.

It sounded intriguing, so I followed the link to Felienne’s paper – Detecting and Visualizing Inter-worksheet Smells in Spreadsheets.

Code Smells

The starting point for the paper is the code smell metaphor introduced in Martin Fowler’s book, Refactoring: Improving the Design of Existing Code.

I don’t have that book, so I visited Wikipedia, to see what it knew about code smells.

Wikipedia Code Smells

Fortunately, Wikipedia had a helpful summary of common code smells, and I’ve listed a few of them below. Can you see how these code smells relate to Excel, whether you’re building worksheets, or creating Excel VBA code?

  • Duplicated code: identical or very similar code exists in more than one location.
  • Long method: a method, function, or procedure that has grown too large.
  • Contrived complexity: forced usage of overly complicated design patterns where simpler design would suffice.
  • Excessive use of literals: these should be coded as named constants, to improve readability and to avoid programming errors.

Hmmm…replace “code” with formulas, and you’ve probably seen (or created) workbooks that had those code smells.

I’ve been guilty of creating some of those smells, and have seen workbooks start small, and slowly grow out of control.

Spreadsheet Smells

Among the most frequent spreadsheet smells that Felienne and her colleagues found were:

  • Inappropriate Intimacy – a worksheet that is overly related to a second worksheet.
  • Feature Envy – if there is a formula that is more interested in cells from another worksheet, it would be better to move the formula to that worksheet
  • Shotgun Surgery – a formula F that is referred to by many different formulas in different worksheets…chances are high that many of the formulas that refer to F will have to be changed if F is changed.

Read More About It

If you’d like to learn more about spreadsheet code smells, take a look at the Spreadsheet Smells paper written by Felienne and her colleagues, to see how their research was done, and what their conclusions were.

You can also read other papers that Felienne has written on this topic, if you’d like to learn more: Felienne Hermans Publications

Have you read anything similar, or heard about code smells before?
_______________

Excel Pivot Table Selection Quick Tip

To format a pivot table, you can select a specific section, such as one of the fields, or a grand total. When you point to a field heading, a black arrow will appear, if the Enable Selection setting is turned on.

Black Arrow Pointer

In the screen shot below, you can see the black arrow at the top of the Product field. Click in that spot, and all the Product item labels are selected.

pivotselect01

Click in that spot again, and the Product heading is selected, instead of the item labels.

pivotselect02

Pivot Table Field Setting Quick Tip

Instead of a single click on a heading cell, you can point to an outer field heading and double-click when the black arrow appears.

In the screen shot below, the black arrow is on the Bran product heading cell.

Note: This trick won’t work on an inner field, like Region, which has no other fields under it.

pivotselect03

Open Field Settings

Double-click on the outer field heading, and the Field Settings dialog box opens.

In there, you can change the layout and other settings, and add or remove subtotals.

pivotselect04

Right-Click Menu

Another way to open the Field Settings dialog box is to right-click on an item, and click Field Settings in the popup menu. This works for both inner and outer fields in the pivot table.

pivotselect05

I find the double-click shortcut to be quicker and easier – as long as you remember to point somewhere that the black arrow appears.

Watch the Pivot Table Selection Video

To see the steps for selecting section of an Excel Pivot Table, you can watch this short video tutorial.

_________________

Add New ComboBox Items in Excel UserForm

If you want to enter data in an Excel worksheet, while keeping the data sheet hidden, you can create an Excel UserForm.

I’ve updated my sample file, so you can now add new parts to the drop down list, while you’re  entering data. It’s almost working the way it should, but I’m stuck on one step, so if you have a solution, please let me know!

[Update: Problem solved with a workaround — see below.]

Select Part from ComboBox Drop Down List

In the sample file, you can click the Add Parts Information button on the worksheet, to open the UserForm.

Then, at the top of the UserForm, select a Part ID from the combo box drop down list.

The drop down list shows part ID, and the part name. After you make a selection, only the part ID appears in the combo box.

userformcomboadd02

The Parts List

On another sheet in the workbook, there are two lists – Location, and Parts. These are dynamic named ranges, based on a formula, and the named ranges will expand automatically, as new items are added to the lists.

userformcomboadd01

Add a New Part to the List

In the latest version of the sample file, you can add new parts to the list, while you are entering data in the UserForm.

  1. First, if the Part ID that you want is not in the list, type it into the Part ID combo box.
  2. Next, when you press the Tab key, to move to the next control, a Part Description text box will appear.
  3. Enter the description, then fill in the rest of the data.
  4. Finally, click the Add This Part button

userformcomboadd05

Select the New Part

After you click the Add This Part button, the new item is added to the Parts List, and the Parts list on the worksheet is sorted A-Z, based on the PartID column.

userformcombo06

The next time you click the Part ID combo box arrow, you will see that the new item now appears in the drop down list.

userformcomboadd04

SetFocus Problem

My goal was to have the Part Description activated, as soon as it was made visible. However, the VBA code wouldn’t cooperate, so I’ve commented out the following line in the code:

Me.txtPartDesc.SetFocus

If you have a solution for getting that line to work, please share it in the comments, or send me an email. I’d appreciate it!

Set Focus Workaround

Update: Thanks to JeanMarc, Jon and Dave, the tab order is working now. You can see their suggestions in the comments below.

  • Instead of being hidden, the Parts Description textbox moves to the far right, so it’s not in the visible part of the form, then moves back when needed.
  • To keep the tab key from stopping on the “off form” textbox, its position is checked. If the textbox is at the far right, go to the next control.

Download the Sample File

To get the sample file, and to check the Excel VBA code, you can download the file from my Contextures website.

On the Sample Excel Files page, in the UserForm section, look for UF0017 – Parts Database with Updateable Comboboxes

The file is available in Excel xlsm or Excel xls format, and zipped. The workbook contains macros, so enable those if you want to test the UserForm combo box code.
_____________________

Excel Data Validation Combo box Codes

Instead of selecting a product code in an Excel drop down list, it’s usually easier to select a product name instead.

However, your pricing calculations might use the product code, instead of the product name, so you need that information.

Show Description, Enter Code

With a bit of programming in Excel, you can show a description in a combo box, but enter the matching code for the selected item into the cell. You’ve seen other versions of my data validation combo box code:

Keep reading, to see how the new combo box codes example works.

Select From a Combo Box

I’ve uploaded a new sample file on the Contextures website, which lets you select a month name, or weekday name, from an Excel combo box.

Select From a Combo Box
Select From a Combo Box

When you leave the combo box, the month number, or weekday code is automatically entered in the cell, instead of the full description.

datavalcomboboxcodes02

The Named Ranges

In the data validation cells, lists are allowed, and they refer to the named ranges – DayList and MonthList – where the short codes are stored.

Named Ranges on Worksheet
Named Ranges on Worksheet

Another range is created for each list, to include the description column, and these names end with “Codes” – DayListCodes and MonthListCodes.

These “Codes” ranges are used to fill the combo box, which is formatted with 2 columns. The first column has a width of zero, so the codes aren’t visible.

datavalcomboboxcodes04

Download the Sample File

To test the combo box codes example, and see how it works, you can download the sample file from the Contextures website.

In the Data Validation section look for DV0057 – Data Validation Combobox Codes.

The file is in Excel 2007/2010 format, and contains macros.
______________________

When Good Excel Macros Go Bad

iconmacrosWhy do good Excel macros suddenly go bad? Does that ever happen to you? Here’s my latest adventure, and maybe it will help you prevent (or solve) a similar problem.

Unprotect Sheets With a Macro

When making changes to client files, I use macros to quickly and easily protect or unprotect all the sheets in an Excel file, like the examples shown below.

  • This macro protects all the worksheets in the active workbook, with no password.
Sub ProtectAllSheetsNoPwd()
Dim ws As Worksheet
On Error Resume Next
For Each ws In ActiveWorkbook.Worksheets
  ws.Protect DrawingObjects:=True, _
    Contents:=True, Password:=""
Next ws
End Sub
  • And this macro unprotects all the worksheets, with no password.
Sub UnProtectAllSheetsNoPwd()
Dim ws As Worksheet
On Error Resume Next
For Each ws In ActiveWorkbook.Worksheets
  ws.Unprotect
Next ws
End Sub

The Protect Macro Fails

However, even a simple macro can run into problems, as I’m sure you’ve already discovered with your own macros. It worked yesterday, but the macro won’t run correctly today, in the same file. And you’re sure that nothing was changed in the file – so what’s the problem?

That’s what happened to me recently, while trying to unprotect a file, so I could make some changes. The macro ran, but the sheet’s weren’t unprotected.

Excel error

I commented out the macro’s error handling, to try and solve the problem, and this run-time error 1004 popped up:

Method ‘Unprotect’ of object ‘_Worksheet’ failed.

Excel error: Method 'Unprotect' of object '_Worksheet' failed
Excel error: Method ‘Unprotect’ of object ‘_Worksheet’ failed

Prevent the Macro Problem

After a bit of head scratching, I realized what the problem was – I had grouped some of the sheets, because they all needed a formatting change. Oops! Because the sheets were grouped, they couldn’t be unprotected.

I added a line of code to the macro, to ungroup the sheets, if necessary, and the problem is solved. You could do something fancier, like identifying the active sheet, and selecting it, instead of the first sheet.

Revised Excel VBA Code

Sub ProtectAllSheetsNoPwd()
Dim ws As Worksheet
On Error Resume Next
Sheets(1).Select '<====== selects first sheet in workbook
For Each ws In ActiveWorkbook.Worksheets
  ws.Protect DrawingObjects:=True, _
    Contents:=True, Password:=""
Next ws
End Sub

The same line was added to the Unprotect macro.

Sub UnProtectAllSheetsNoPwd()
Dim ws As Worksheet
On Error Resume Next
Sheets(1).Select '<====== selects first sheet in workbook
For Each ws In ActiveWorkbook.Worksheets
  ws.Unprotect
Next ws
End Sub

Watch the Protect Macro Fails Video

To see the problem that occurs when you try to protect or unprotect group sheets, and the code change that fixes it, you can watch this short Excel video tutorial.

___________

Get Good Dates With Excel Data Validation

imageThere’s a story making the rounds, about a guy who rated all his online dating prospects, by using an Excel worksheet.

While I’m sure we could all learn a thing or two from that guy’s file, this article is about a different kind of dates – calendar dates.

Calendar Dates in Excel

Calendar dates might not be as exciting, but unlike romantic dates, you can use data validation to help keep them under control in Excel.

Entering Period End Dates

Last week in the Daily Dose of Excel blog, Dick Kusleika was looking for a more efficient way to enter a Period Ending date in his time sheet.

Every two weeks he sets up a new workbook, and has to add two weeks to the previous end date.

Sometimes it’s easy to do that kind of math in your head, but if you’re rolling into a new month, the calculation can be trickier.

So, Dick changed the date cell to a formula, and just added 14 to that calculation every two weeks.

image

There were a few suggestions for solving the problem, but Dick wanted something simple and static — the date cell couldn’t change, if you opened the file on a different date.

Create a Dynamic List of Dates

My suggestion was to create a drop down list of Period End dates, and select the next date from that list. With some formulas in the background, the list of valid dates would update automatically.

However, when you select one of those dates in the Period End cell, it’s a static entry in that cell.

Create List of Dates

The first step is to create the list of dates, based on Dick’s original Period End date – October 13, 2010.

  • On Sheet2, put the starting date in cell C2: =DATE(2010,8,13)
  • In cell C3, enter a formula to calculate the current date: =TODAY()
  • In C6, enter this formula, to calculate the current pay period’s week end:
    =C2+INT((C3-C2)/14)*14

datavalidationdates16

  • In C5, subtract 14 days, to calculate the previous period end date: =C6-14
  • In C7, add 14 days, to calculate the next period end date: =C6+14

Name the Date List

Next, you’ll name the date list, so you can use it in a data validation drop down.

  • Select cells C5:C7, and click in the Name Box, to the left of the Formula bar
  • Type a one-word name for the list – DateList – and press the Enter key.

datavalidationdates17

Create the Drop Down List of Dates

Back on the Time Sheet, you’ll add a data validation drop down list in the date cell.

  • On the Time Sheet, select the Period End date cell.
  • On the Excel Ribbon, click the Data tab, and click Data Validation.
  • From the Allow drop down, click on List
  • In the Source box, type an equal sign and the list’s name: =DateList
  • Click OK to close the data validation dialog box.

datavalidationdates13

Test the Drop Down List of Dates

On the Time Sheet, the Period End date cell now has a drop down arrow.

  • Click the arrow, to see the current list of dates, and click on a date to select it.

datavalidationdates18

The selected date is entered in the cell, as a value, not a formula. Even when the list of dates changes, the selected date will not be affected.

More Examples

Please visit the Contextures website for more examples of Excel Data Validation for dates.

__________________

How to Show Excel Preview Picture When Opening Files

When you’re opening files in Excel, you can see the file Details, or the icons, or select another way to look at the list, such as Preview.

That Preview option sounds promising, but instead of a picture of the file’s contents, you usually see this message instead – Preview Not Available.

And that’s not much help. Here’s how to show Excel preview picture when opening files

Continue reading “How to Show Excel Preview Picture When Opening Files”