The Task
Create an Excel spreadsheet that includes a formula designed to pick a day out of the week at random using spreadsheet software such as Microsoft Excel.You will need to use one of Excel's random functions to generate at least one random number as the basis of this exercise. That random number or perhaps a series of random numbers will then be needed to pick one of the seven days of the week (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday).
My Solution
I decided to pursue multiple options for this challenge. I created four different formulas. My solutions have been included in an Excel file that can be found at: https://1drv.ms/x/s!AL3VXpGA-24u3UMOne thing that I prefer to do in spreadsheets is to keep tables that are being used for references separate from the work. I have a worksheet named Tables. In that, I created a table named tblWeekDays for the days of the week. I added headers showing that one column is for Values and the other is for the day. I also have another for random numbers, but I'm not yet ready to discuss that one. For the table's data, I added 1 through 7 for values and Sunday through Saturday for the days.
My formulas have been added to a worksheet predictably called Formulas. For each formula, I have put big and bold text to start each formula. I wrote the formula that shows a random day on the Random Day line. The formula was then copied and pasted to the Formula line. An apostrophe was added before the formula to tell Excel to treat the formula as text instead of a formula.
My first solution is simple. I used a VLOOKUP combined with a RANDBETWEEN function. I randomly pick a number from 1 to 7. From there, I check for the value in tblWeekDays and return the data from the 2nd column, which is the day of the week. Although the table is sorted by value, I still feel that it's good form to look for an exact match. You never know when someone is going to change the sorting on you. That's why I finished with the FALSE.
=VLOOKUP(RANDBETWEEN(1,7),tblWeekDays,2,FALSE) My second formula reduces how much of a table is required. This approach does not require the values table. It only looks at the Day column. The formula starts at the header for the Day column, then checks for the value at a cell in the row randomly picked from 1 to 7 below it in the same column (the final 0 indicates the change in the column). I selected the header in the formula by clicking on it. The table references should be easy to figure out, but there are other ways to handle this one. I could have easily used Tables!$B$1 instead. That would have made an absolute reference to the same cell that is currently occupied by the header. This also does not require that the table is actually identified as a table. Another option would have been to use the initial reference as Tables!$B$2. If we also changed the RANDBETWEEN function to look for a value from 0 to 6 instead of 1 to 7, this would allow us to enter the formula without bothering with the header.
|
=CHOOSE(RANDBETWEEN(1,7),"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday") My final formula took a different approach. Instead of using a single RANDBETWEEN function, I added random numbers to each day in the day of the week table. This was done by adding the Random column and adding a RAND function for each of them. There are ways that this approach can evolve that would not be possible with the previous formulas. Like my second formula, I used the OFFSET function with a start at tblWeekDays[[#Headers],[Day]]. Instead of using RANDBETWEEN to pick which day we are using, I looked for the largest of the random values by using the MAX function. Alternatively, MIN would be just as effective. I used the MATCH function to find where in the list of random values that exact (the final 0 in the MATCH function and the next-to-last 0 overall specifies an exact match) maximum value is located. From there, Excel will look down from the Day header as many rows as that value is located, and stay on the Day column.
|
No comments:
Post a Comment