Do you use Excel to keep track of upcoming payments, or other dates? To make that list more helpful, you can highlight upcoming dates in Excel. In this example, we’ll highlight dates if they’re within the next couple of weeks.
Add Products to Excel Order Form
The summer is off to a good start here in Canada – we had perfect weather for our first long weekend of the season. My garden is almost ready, and I haven’t killed any of the plants yet!
Over the weekend, my Contextures website and blog were moved to a different web host and servers. It seems to have gone smoothly, but please let me know if you see anything strange. Well, stranger than usual. 😉
Click to Add Products to an Order
As you know, when you’re moving, you sometimes find interesting things in the back of the closet. While I was checking the website, I found this sample file, that lets you click to add products to an Excel order form.
On the Data Entry sheet, you can select a product category, from the drop down list.

When a category is selected, the Excel Worksheet_Change event code runs, and lists all the products in the selected category.
Worksheet Change VBA Code
The code uses an Advanced Filter to copy the products onto the Data Entry sheet.

In the list of products, click on any row, in column G, to add that product to the order form.

The Order Form Move Code
Another event code runs when you click on a cell in column G – the SelectionChange event.
It checks the column and row number of the cell that was selected, and checks for an entry in column A.

If the selection was in column G and the row number is greater than 6, and there is something in column A, an X is added to the cell.
Then, the MoveRow code runs, and it copies the selected row to the order form, in the row below the previous product ordered.

Here is the OrderForm sheet, with condiments and dairy products listed.

Future Enhancements to the Product List Order Code
When you go back an look at an Excel project, you can usually think of several things that would improve it. In this example, I’d like to add code that prints the completed order form, and clears the OrderForm sheet.
Is there anything else that you’d add or change?
Download the Product List Order Workbook
To download the product list order form workbook, you can visit the Sample Files page on the Contextures website. In the Filters section, look for FL0016 – Move Items to Order Form.
The file is in Excel 2003 format, and zipped. It contains macros, so enable those, if you want to test the code.
Another Excel Order Form
To see another example of an Excel order form, go to the Excel Order Form page on my Contextures site.
In the video below, I give a quick demo of how that order form works.
_________________
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.

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

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.

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.

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.

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.
_________________
Show Data From Hidden Rows in Excel Chart
You can add a chart in Excel, based on worksheet data, but if you filter the data, and rows are hidden, that data also disappears from the chart. See how to prevent that problem from happening.
Continue reading “Show Data From Hidden Rows in Excel Chart”
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.

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.

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.
- First, if the Part ID that you want is not in the list, type it into the Part ID combo box.
- Next, when you press the Tab key, to move to the next control, a Part Description text box will appear.
- Enter the description, then fill in the rest of the data.
- Finally, click the Add This Part button

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.

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.

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:
- Double-click on data validation cell to open combo box
- Click on data validation cell to open combo box
- Use named lists with data validation combo box
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.

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

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.

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.

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.
______________________
Change Font in Excel Column Headers
Do you ever wish that the letters in the Excel column headers were bigger? Did someone send you an Excel file, with a different font in the headers, and you can’t figure out what they did to change font in Excel column headers?

When Good Excel Macros Go Bad
Why 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.

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
There’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.

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

- 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.
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.

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.

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.
__________________