On my pivot table blog yesterday, I wrote an article about using counts in a calculated field. A helpful tip is buried in the 8-minute video that shows how to create the calculation, so I’m posting the tip separately here.
This tip can help prevent data entry problems, if you’ve set up a named Excel table for a client or co-workers to use, or even if you’re the only one using the table. Excel will automatically enter the data for you!
Happy New Year! I hope you had a relaxing end to 2014, and great start to 2015.
If you’re working in Excel at the beginning or end of a year, dates can be a problem. Usually, you can enter the day and month, then press Enter, and Excel will add the year.
Most of the time that works well, but if you’re doing December month end reports in January, Excel will add 2015 – the current year. Remember to manually enter the year to the date, if it’s not the current year – 12/31/14. And that means typing 3 extra characters! Who has time for that?
Today’s challenge is to count how many guests stayed at a hotel, in a specific date range, based on the guest arrival and departure dates.
Find Specific Dates
In previous examples, we’ve seen how to check if a specific date falls within a date range. For example, if you have a list of orders, you can use SUMIF or SUMIFS, to add up all the orders between a start and end date. You can see the written instructions for this on my website.
Guest Visits
It is a little trickier to count hotel guests though, because the booking table only shows the guest arrival and departure dates. We have to use those dates, to see if any part of a guest’s visit overlapped our date range.
For this report, we want the number of guest who were in the hotel during the reporting period of December 7th to 9th. I’ve highlighted the records that should be included, when we create a formula to count the guests.
When Did the Guest Leave?
To calculate if the guest’s visit was within the date range, we’ll start with two short formulas.
The first formula will check the guest’s departure date – was it on or after the start of our date range? (If they left before the date range started, they won’t be counted)
The report start date is in cell C3, and guest’s departure date is in cell C6, so the formula is:
=C6>=$C$3
The first 5 guests all left before December 7th, so the result in those rows is FALSE.
When Did the Guest Arrive?
The second formula will check the guest’s arrival date – was it on or before the end of our date range? (If they arrived after the date range ended, they won’t be counted)
The report start date is in cell D3, and guest’s arrival date is in cell B6, so the formula is:
=B6<=$D$3
Guest #11 arrived on December 10, and that is after the report end date of December 9th, so the result in that row is FALSE.
Both Results TRUE
If the result of both formulas is TRUE, then the guest stayed at the hotel during the reporting period:
they arrived on or before December 9th
they departed on or after December 7th
We’ll use a SUMIFS formula to check those two columns, and sum then number of guests in column E. I’ve changed the column headings in F and G, to Dep Check and Arr Check.
The result is 11, and that matches the total when the numbers are selected in the manually highlighted rows. You can see that automatic calculation in the Status Bar.
Download the Sample File
To see how the formulas work, you can download the sample file from my Contextures website. On the Excel Sample Files page, go to the Functions section, and look for FN0036 – Count Hotel Guests in Date Range.
The zipped file is in xlsx format, and does not contain macros. It also has another, more complex, example of counting in a date range. Guests are counted by their hotel loyalty program level, and the number of nights booked in the date range is also calculated.
Video: Count Numbers in a Range – COUNTIFS
Using a simpler example, this video shows how to use the COUNTIFS function to count cells based on a range of numbers.
The minimum and maximum numbers are entered on the worksheet, so it’s easy to change the number range, when needed.
Thank you for reading my Excel Roundup every Monday, and I will be taking a couple of weeks off, to enjoy the holidays. There will be a blog post here this Thursday, and the next roundup will be posted on January 5th.
To help you celebrate, here is a no-macro animated Christmas tree, created in Excel, of course. Just move the scroll bar, to add lights, tinsel, a star, and presents, to the tree. You could use this technique for less festive projects too, like a business dashboard. Read the details on my Contextures website.
It’s hard enough to select one item from a long drop down list – how can we make it easier to select multiple items for a cell?
In the screen shot below, the worksheet has code that lets you select multiple item from the drop down list. Each new item is added to the cell, instead of replacing the previous selection.
Use a ListBox
To see more of the list, and select multiple items at once, you can use a ListBox, embedded in a UserForm. When you click OK, all of the selected items are entered in the cell, separated by commas.
This technique works for drop down lists that are based on a named range, such as MonthList, or AllProducts.
Really Long List
However, if your list has thousands of items, it can take a while to scroll through a ListBox too. So, to make it easier to check items in the list, I’ve created a new sample file, based on the previous ListBox file.
It has a combo box at the top of the UserForm, and you can start typing an item name there. When the correct item shows up, click Add, or press the Enter key, to add it to the list.
You can also use the ListBox, to check or uncheck items.
When you’ve finished selecting items, click OK, to add all the items to the cell.
In the sample file, there are two versions of the technique –
one opens the ListBox UserForm when you click on a cell with a drop down list
one opens the ListBox UserForm when you double-click on a cell with a drop down list
Download the Sample File
To see how this technique works, you can download the sample file from my Contextures website. On the Sample Files page, go to the Data Validation section, and look for DV0067 – Select Multiple Items in ComboBox or ListBox
The zipped file is in xlsm format, and contains macros. Enable the macros, when you open the file, if you want to test the code.
To see the code, right-click on the DataEntry_Click or DataEntry_DoubleClick sheet tab then click View Code.
Christmas is only 2-1/2 weeks away, so it’s time to start planning! Of course you’ll want to organize everything in a spreadsheet, so you can download the free Excel Holiday Planner, from my Contextures website.
It has shopping lists, budget sheets, task lists, and even a dinner planner, so get your copy, and get the holidays under control.
If you’ve got a long list of items, it can take a while to find what you’re looking for, in a data validation drop down list.
For example, in the screen shot below, you’d have to scroll down to the bottom of this long list, if you want to select the Teatime Chocolate Biscuits (and don’t pick the Tofu by mistake!)
Happy Cyber Monday! I hope you didn’t spend all you money at the Black Friday sales, and have a little left over for today’s shopping spree.
To celebrate Cyber Monday, the busiest online shopping day of the year in the USA, you can buy a couple of my Excel products, at 50% off today only. The details, and links to the product pages, are below.
NOTE: Unfortunately, the shopping cart will only discount one item per order — if you want both items, please order them separately.
Wow! It’s December already – how did that happen? It’s Cyber Monday too – a big online shopping day. Did you find any Excel bargains?
To start the countdown to Christmas, Mynda Treacy created an Advent Calendar in Excel. You can download the file, if you’d like a bit of fun while you count the days until Christmas
You’ll have to answer a quiz each day, to open the calendar doors. To get a head start, you can start thinking about the answer to the December 5th question, shown in the screen shot below.