Wednesday, 7 December 2016

Date function

DateDif

To get the number of days, weeks or years between two dates in Excel, use the DATEDIF function. The DATEDIF function has three arguments.
1. Fill in "d" for the third argument to get the number of days between two dates.
Days
Note: =A2-A1 produces the exact same result!
2. Fill in "m" for the third argument to get the number of months between two dates.
Months
3. Fill in "y" for the third argument to get the number of years between two dates.
Years
4. Fill in "yd" for the third argument to ignore years and get the number of days between two dates.
Ignore Years, Get Days
5. Fill in "md" for the third argument to ignore months and get the number of days between two dates.
Ignore Months, Get Days
6. Fill in "ym" for the third argument to ignore years and get the number of months between two dates.
Ignore Years, Get Months
Important note: the DATEDIF function returns the number of complete days, months or years. This may give unexpected results when the day/month number of the second date is lower than the day/month number of the first date. See the example below.
Unexpected Result
The difference is 6 years. Almost 7 years! Use the following formula to return 7 years.

Weekday Function

1. The WEEKDAY function in Excel returns a number from 1 (Sunday) to 7 (Saturday) representing the day of the week of a date. Apparently, 12/16/2013 falls on a Monday.
Weekday Function in Excel
2. You can also use the TEXT function to display the day of the week.
Text Function
3. Create a custom date format (dddd) to display the day of the week.
Custom Date Format

Networkdays Function

1. The NETWORKDAYS function returns the number of weekdays (weekends excluded) between two dates.
Networkdays Function
2. If you supply a list of holidays, the NETWORKDAYS function returns the number of workdays (weekends and holidays excluded) between two dates.
Workdays
The calendar below helps you understand the NETWORKDAYS function.
Calendar
3. Dates are stored as numbers in Excel and count the number of days since January 0, 1900. Instead of supplying a list, supply an array constant of the numbers that represent these dates. To achieve this, select E1:E2 in the formula and press F9.
Array Constant

Workday Function

The WORKDAY function is (almost) the opposite of the NETWORKDAYS function. It returns the date before or after a specified number of weekdays (weekends excluded).
Workday Function
Note: the WORKDAY function returns the serial number of the date. Apply a Date format to display the date.
The calendar below helps you understand the WORKDAY function.
Calendar
Again, if you supply a list of holidays, the WORKDAY function returns the date before or after a specified number of workdays (weekends and holidays excluded).

Year Function Twice
Days until Birthday

To calculate the number of days until your birthday in Excel, execute the following steps.
1. Select a cell and enter your date of birth.
2. Select the cell next to it and enter the TODAY function to return today's date.
Today
3. The most difficult part in order to get the number of days until your birthday is to find your next birthday. The formula below does the trick.
Next Birthday
Explanation: The DATE function accepts three arguments: year, month and day. We used the DATEDIF function to find the number of complete years ("y") between Date of Birth and Today. DATEDIF(A2,B2,"y") equals 32. If 32 complete years have passed since your date of birth (in other words, you have already celebrated your 32st birthday), your next birthday will be 32 + 1 = 33 years after your date of birth.
4. Next, we use the DATEDIF function to find the number of days ("d") between Today and Next Birthday.
Days until Birthday in Excel
Time Sheet

This example teaches you how to create a simple timesheet calculator in Excel. Cells that contain formulas are colored light yellow. If you are in a hurry, simply download the Excel file.
1. To automatically calculate the next 4 days and dates when you enter a start date, use the formulas below.
Date Formulas
2. Select the cells containing the times.
Select Time Cells
3. Right click, click Format Cells, and select the right Time format. Use the circled format for cell K12, K13, K14.
Time Format
4. To automatically calculate the hours worked each day, the total hours and the overtime hours, use the formulas below.
Time Formulas
Last Day of the Month

To get the date of the last day of the month in Excel, use the EOMONTH (End of Month) function.
1. For example, get the date of the last day of the current month.
Last Day, Current Month
Note: the EOMONTH function returns the serial number of the date. Apply a Date format to display the date.
2. For example, get the date of the last day of the next month.
Last Day, Next Month
3. For example, get the date of the last day of the current month - 8 months = 6 - 8 = -2 = October (-2+12=10), 2011!
Last Day, 8 Months Before Current Month
Holidays

This example teaches you how to get the date of a holiday for any year. If you are in a hurry, simply download the Excel file.
Before you start: the CHOOSE function returns a value from a list of values, based on a position number. For example, =CHOOSE(3,"Car","Train","Boat","Plane") returns Boat. The WEEKDAY function returns a number from 1 (Sunday) to 7 (Saturday) representing the day of the week of a date.
1. This is what the spreadsheet looks like. If you enter a year into cell C2, Excel returns all the holidays for that year. Of course, New Year's Day, Independence Day, Veteran's Day and Christmas Day are easy.
Holidays in Excel
2. All other holidays can be described in a similar way: the xth day in a month (except Memorial day which is slightly different). Let's take a look at Thanksgiving Day 2016. If you understand Thanksgiving Day, you understand all holidays. Thanksgiving is celebrated the 4th Thursday in November.
Thanksgiving Day 2016
The calendar below helps you understand Thanksgiving Day 2016.
Calendar November 2016
Explanation: DATE(C2,11,1) reduces to 11/1/2016. WEEKDAY(DATE(C2,11,1)) reduces to 3 (Tuesday). Now the formula reduces to 11/1/2016 + 21 + CHOOSE(3,4,3,2,1,0,6,5) = 11/1/2016 + 21 + 2 = 11/24/2016. We needed the 2 extra days because it takes 2 days until the first Thursday in November. From there, it takes another 21 days (3 weeks) until the 4rd Thursday in November. It doesn't matter on which day November 1 falls, the CHOOSE function correctly adds the number of days until the first Thursday in November (notice the pattern in the list of values). From there, it always takes another 21 days until the 4rd Thursday in November. Therefore, this formula works for every year.
3. Let's take a look at Martin Luther King Jr. Day.
Martin Luther King Jr. Day 2016
Explanation: this formula is almost the same. The first DATE function reduces to the first of January this time. The base position (0) in the list of values for the CHOOSE function is located at the second spot now (we are looking for a Monday). If the 1st of January falls on a Monday, it takes 14 + CHOOSE(2,1,0,6,5,4,3,2) = 14 + 0 = 14 days (2 weeks) until the 3rd Monday in January.
Quarter

An easy formula that returns the quarter for a given date. There's no built-in function in Excel that can do this.
1. Enter the formula shown below.
Quarter Formula in Excel
Explanation: ROUNDUP(x,0) always rounds x up to the nearest integer. The MONTH function returns the month number of a date. In this example, the formula reduces to =ROUNDUP(5/3,0), =ROUNDUP(1.666667,0), 2. May is in Quarter 2.
2. Let's see if this formula works for all months.
Months
Explanation: now it's not difficult to see that the first three values (months) in column B are rounded up to 1 (Quarter 1), the next three values (months) in column B are rounded up to 2 (Quarter 2), etc.
Day of the Year

An easy formula that returns the day of the year for a given date. There's no built-in function in Excel that can do this.
1. Enter the formula shown below.
Day of the Year in Excel
Explanation: Dates and times are stored as numbers in Excel and count the number of days since January 0, 1900. June 23, 2012 is the same as 41083. The DATE function accepts three arguments: year, month and day. DATE(YEAR(A1),1,1) or 1-jan-2012 is the same as 40909. Subtracting these numbers (41083 - 40909 = 174) and adding 1 gives the day of the year.

Text Functions


Excel has many functions to offer when it comes to manipulating text strings.

Join Strings

To join strings, use the & operator.
Join Strings
Note: to insert a space, use " "

Left

To extract the leftmost characters from a string, use the LEFT function.
Left Function

Right

To extract the rightmost characters from a string, use the RIGHT function.
Right Function

Mid

To extract a substring, starting in the middle of a string, use the MID function.
Mid Function
Note: started at position 5 (p) with length 3.

Len

To get the length of a string, use the LEN function.
Len Function
Note: space (position 8) included!

Find

To find the position of a substring in a string, use the FIND function.
Find Function
Note: string "am" found at position 3.

Substitute

To replace existing text with new text in a string, use the SUBSTITUTE function.
Substitute Function
Separate Strings

This example teaches you how to separate strings in Excel.
Separate Strings Example
The problem we are dealing with is that we need to tell Excel where we want to separate the string. In case of Smith, Mike the comma is at position 6 while in case of Williams, Janet the comma is at position 9.
1. To get the first name, use the formula below.
First Name
Explanation: to find the position of the comma, use the FIND function (position 6). To get the length of a string, use the LEN function (11 characters). =RIGHT(A2,LEN(A2)-FIND(",",A2)-1) reduces to =RIGHT(A2,11-6-1). =RIGHT(A2,4) extracts the 4 rightmost characters and gives the desired result (Mike).
2. To get the last name, use the following formula.
Last Name
Explanation: to find the position of the comma, use the FIND function (position 6). =LEFT(A2,FIND(",", A2)-1) reduces to =LEFT(A2,6-1). =LEFT(A2,5) extracts the 5 leftmost characters and gives the desired result (Smith).
3. Select the range B2:C2 and drag it down.
Separate Strings Result
Number of Instances

This example describes how to count the number of instances of text (or a number) in a cell.
1. Use the LEN functon to get the length of the string (25 characters, including spaces).
Len Function in Excel
2. The SUBSTITUTE function replaces existing text with new text in a string. LEN(SUBSTITUTE(A1,B1,"")) equals 13 (the length of the string without the words dog). If we subtract this number from 25, we get the length of the dog instances (25-13=12).
Substitute Function
3. Dividing this number by the length of the word dog (3), gives us the dog instances (12/3=4).
Count
Number of Words

This example describes how to count the number of words in a cell.
1a. The TRIM function returns a string with extra spaces, starting spaces and ending spaces removed.
Trim Function
1b. To get the length of the string with normal spaces, we combine the LEN and TRIM function.
Len and Trim Function
2a. The SUBSTITUTE function replaces existing text with new text in a text string. We use the SUBSTITUTE function to get the string without spaces.
Substitute Function
2b. To get the length of the string without spaces, we combine the LEN and SUBSTITUTE function.
Len and Substitute Function
3. Now comes the simple trick. To get the number of words, we subtract the length of the string without spaces (10) from the length of the string with normal spaces (12) and add 1.
Number of Words
Text to Columns

To separate the contents of one Excel cell into separate columns, you can use the 'Convert Text to Columns Wizard'. For example, when you want to separate a list of full names into last and first names.
1. Select the range with full names.
Text to Columns Example
2. On the Data tab, click Text to Columns.
Click Text to Columns
The following dialog box appears.
3. Choose Delimited and click Next.
Convert Text to Columns Wizard - Step 1
4. Clear all the check boxes under Delimiters except for the Comma and Space check box.
5. Click Finish.
Convert Text to Columns Wizard - Step 2
Note: This example has commas and spaces as delimiters. You may have other delimiters in your data. Experiment by checking and unchecking the different check boxes. You get a live preview of how your data will be separated.
Result:
Text to Columns Result
Lower/Upper Case

This example teaches you how to convert a text string to lower, upper or proper case in Excel.
1. Use the LOWER function to convert all letters in a text string to lowercase.
Lower Function in Excel
2. Use the UPPER function to convert all letters in a text string to uppercase.
Upper Function in Excel
3. Use the PROPER function to convert a text string to proper case. That is, the first letter in each word in uppercase, and all other letters in lowercase.
Proper Function in Excel
Remove Unwanted Characters

The TRIM function returns a string with extra spaces, starting spaces and ending spaces removed. The CLEAN function removes nonprintable characters from a string.
1. For example, cell A1 below contains the string " Excel   Easy  "
Trim Function in Excel
Note: the TRIM function returns the string "Excel Easy".
2. For example, cell A1 below contains a nonprintable character.
Clean Function in Excel
Note: the CLEAN function removes this nonprintable character. Text imported from other applications may contain nonprintable characters.
Compare Text

This example shows two ways to compare text in Excel. One is case-sensitive and one is case-insensitive.
1. Use the EXACT function (case-sensitive).
Case-sensitive
2. Use the formula =A1=B1 (case-insensitive).
Case-insensitive
Find vs Search

The FIND function and the SEARCH function are very similar to each other. This example shows the difference.
1. To find the position of a substring in a string, use the FIND function. FIND is case-sensitive.
Find Function in Excel
2. To find the position of a substring in a string, use the SEARCH function. SEARCH is case-insensitive.
Search Function in Excel
Note: string "excel" found at position 11. Even though it's actually the string "Excel"
3. The SEARCH function is more versatile. You can use wildcard characters when you use this function.
Asterisk
It also finds Microsoft Word 2010
Note: A question mark (?) matches exactly one character. An asterisk (*) matches a series of zero or more characters.
4. One other interesting point to mention about the FIND and the SEARCH function is that they have a 3rd optional argument. You can use this argument to indicate the position, counting from the left, at which you want to start searching.
Start Searching at Position 1
Note: string "o" found at position 5.
Start Searching at Position 6
Note: string "o" found at position 7 (started searching at position 6).
Substitute vs Replace

This example shows the difference between the SUBSTITUTE function and the REPLACE function.
1a. If you know the text to be replaced, use the SUBSTITUTE function.
Substitute Function in Excel
1b. The SUBSTITUTE function has a 4th optional argument. You can use this argument to indicate which occurrence you want to substitute.
Substitute the Second Occurrence
2. If you know the position of the text to be replaced, use the REPLACE function.
Replace Function in Excel
Note: started at position 1 with length 3.


No comments:

Post a Comment