Pages

Showing posts with label Formulas. Show all posts
Showing posts with label Formulas. Show all posts

Retrieve the value from every Nth row in Excel

Imagine that you want to retrieve the values from every 5th row in a certain range of your Excel worksheet.


In case your range is placed on column A:
  1. Edit the first cell on the in the column where you want to have the list of every 5th row of column A
  2. Write the following formula.

    =IF(ROW(A1)*5-5>ROWS($A$4:$A$50),"",INDEX(A$4:A$50,ROW(A1)*5-5))
     
  3. Copy the formula all the way down to as many cells as your range rows divided by 5.
  4. There you have it.
Note: This example shows you how to retrieve values from every 5th row in Excel but you can do this for any number. To do so just replace the 5-5 by the number you want.

Now let's break down the formula:

The formula starts with a IF function which will test if  ROW(A1)*5-5>ROWS($A$4:$A$50)
Basically this test will avoid a #REF error in case you copy the formula down to more rows than your range divided by 5. If you do, the formula will just retrieve a blank cell.

In case ROW(A1)*5-5>ROWS($A$4:$A$50) is False, then the following condition will be applied:
INDEX(A$4:A$50,ROW(A1)*5-5)

The index function will grab your range A1:A50 and retrieve the values from the following lines:
ROW(A1)*5-5  → 1*5-5 = line 1
ROW(A2)*5-5  → 2*5-5 = line 5
ROW(A3)*5-5  → 3*5-5 = line 10
...
ROW(A11)*5-5  → 11*5-5 = line 50

Do you like this free website? Please share this page on Google+ or Facebook.




Best Wishes for 2016 with an Excel Formula


I've found this formula on Linkedin and I had to share it. It's the perfect way to send Best Wishes to any Excel Geek.

Just copy and Paste the Formula to your Excel :
  • =MID(ADDRESS(1,2^3),2,1)&BIN2HEX(1010)&REPT(CHAR(4^2*(2+3)),2)&CHAR(DEGREES(ASIN(1))-1)&CHAR(CHAR(17*3)&CHAR(5^2*2))&MID(ADDRESS(1,2*6+2),2,1)&BIN2HEX(1110)&CHAR(DEGREES(ATAN(1,732051))+3^3)&CHAR(2+1&2)&MID(ADDRESS(1,4^2+3^2),2,1)&CHAR(DEGREES(ACOS(0,5))+(4-1)^2)&BIN2HEX(1010)&CHAR(BIN2HEX(1000)&BIN2HEX(10)
Impress that friend of yours who is always bragging himself about being an excel expert.


Do you like this free website? Please share this page on Google+ or Facebook.

How to calculate the number of hours between two dates in excel

CONVERT() Excel function can be very usefull in case you need to convert a given number from one measurement system to another.

Syntax:
CONVERT(Number, from_unit, to_unit)

Parameters:
Number -is the value that you want to convert
from_unit - is the unit for number
to_unit - is the unit for the result

Examples of CONVERT Function


Formula: =CONVERT(7;"day";"hr")
Description: Converts 7 days in hours
Result = 168

Formula: =CONVERT(("12-10-2014"-"05-10-2014");"day";"hr")
Description: Converts to hours the interval between 05-10-2014 and 12-10-2014
Result = 168

Formula: =CONVERT(100;"m";"in")
Description: Converts 100 meters in inches
Result = 3937.01

Formula: =CONVERT(A1;"hr";"sec")
Cell A1 = 1
Description: Converts the value in cell A1 from hours to seconds
Result = 3600





How to Find Cells containing Formulas in Excel

Everyone that uses excel on their daily business certainly came accross with spreadsheets with several cells containing formulas. When these spreadsheets have not been created by us, it can be quite a nightmare to identify all the cells that contain formulas.

Excel "Go to Special" is a very usefull tool when it comes to find cells with formulas. Selecting the "Formulas" radio button will enable all cells that contain formulas. After that you can just change the background color or formating of the enabled cells which will stand out the cells with formulas.

Step by step to stand out the cells with formulas in your worksheet:


1 - Go to ‘Home’ tab > ‘Find & Select’ > ‘Go To Special’. Or just press  'F5' > 'Special' to open the "Go To Special" dialog box.

How to find cells with formulas in excel


2 - Select the 'Formulas' radio button, as shown in the picture above and four checkboxes are now enabled (Numbers, Text, Logical, Errors). These checkboxes refer to the return type of the formulas

Example: If you check only the 'Numbers' checkbox then it will search only the cells which contain formulas whose return type is a number.


3 - Now click 'OK' and ALL cells that contain formulas get selected. Please note that in this example all cells with formulas have been selected because we've kept the 4 checkboxes marked.


4 - The last step which will highlight each cell in your spreadsheet that contains a formula is: without clicking anywhere on your speadsheet, change the background color of all the selected cells.


5 - Done. Now all formulas cells are clearly identified.


How to display the week number and year code in Excel


Sometimes we need to aggregate data by week and the Excel function - WEEKNUM works like a charm. But there are situations where we have data referring to diferent years and in these situations we have to associate the week number to the respective year.

For these situations we can combine WEEKUM and TEXT Excel functions. The formula would be as follows:

=TEXT(A1,"yy")&TEXT(WEEKNUM(A1),"00")

How to display the week number and year code, step by step:


  1. Enter 01-01-2014 on cell A1 (date from which will be retrieved the week number and year code)
  2. Enter the above formula in cell B2
  3. Press Enter
  4. B2 should now contain the value  - 1401 - the first two digits "14" refers to year 2014, and  the last two "01" refer to week number of date 01-01-2014


Examples:

Formula to return the week number and year code considering the dates in column B










Now you can play arround with the formula to retrieve year code and week number in the sequence you need.

Retrieve the week number followed by the year code:

=TEXT(WEEKNUM(B3),"00")&TEXT(B3,"yy")

Retrieve the week number and year code with a "-" in the between:

=TEXT(WEEKNUM(B3),"00")&"-"&TEXT(B3,"yy")


Excel Left Lookup Formula Using VLOOKUP Explained

How many times did you needed retrieving values from columns on the left in your excel table ? And how many of those times did you ended up moving columns so that you could use VLOOKUP function ?
Well, that's over... from today onwards you will no longer need to adjust any columns in your excel file. I'll show you how to use VLOOKUP function to retrieve values from columns on the left.

Lookup values on the LEFT


Let's imagine that you have the following table with all sales made by your staff during the last 6 months.
Lookup values on the LEFT

Now, If I wanted to retrieve how many pens have been sold I could easily use VLOOKUP as follows:
=VLOOKUP("Pen",$D$1:$E$1,2,FALSE) and it would retrieve 27 (Cell E5)

But what if I'd need to know in which region have the Pens been sold ? Then I'd have a problem using Vlookup as I'm able to search to the left of Column D!

Well, here's a work arround:
Combine VLOOKUP with CHOOSE function.

Question: In which region have the Pens been sold ?
Fórmula: =VLOOKUP("Pen",CHOOSE({1,2},$D$2:$D$12,$B$2:$B$12),2,FALSE)
Result: Central
Lookup values on the LEFT

Formula explained:
The only part of this formula that should be new for you is the one in bold.
=VLOOKUP("Pen",CHOOSE({1,2},$D$2:$D$12,$B$2:$B$12),2,FALSE)

CHOOSE Function - chooses a  value or action to perform from a list of values, based on an index number.

The job of the CHOOSE function in the formula is to trick VLOOKUP into believing that column 4 is actually column 1. As a result, the Item name can be used as the lookup value to find the name of the part supplied by each company.




Excel Function DATEDIF - Calculate the difference between two dates

DATEDIF is an Excel worksheet function which is undocumented  in the Excel help file. For some reason Microsoft has decided not to document this function in any other versions other than Excel 2000! This function is quite usefull if you need to return the diference between two days in years, months or days.

Syntax

DATEDIF(Start_date, End_date, "Interval")

Parameters

Start_date -  A date that represents the first, or starting, date of the period. Dates may be entered as text strings within quotation marks (for example, "2001/1/30"), as serial numbers (for example, 36921, which represents January 30, 2001, if you're using the 1900 date system), or as the results of other formulas or functions (for example, DATEVALUE("2001/1/30")).

End_date - A date that represents the last, or ending, date of the period.

Interval is the interval type you want returned. The Interval value should be one of:

Unit
Returns
"Y"
Number of complete years between the dates.
"M"
Number of complete months between the dates.
"D"
Number of days between the dates.
"MD"
Number of complete days between the dates as if they were of the same month and same year.
"YM"
Number of complete months between the dates as if they were of the same year.
"YD"
Number of complete days between the dates as if they were of the same year.

Some remarks about DATEFDIF function
  • If Start_Date if greater than End_date, DATEDIF will retunr a #NUM error.
  • If either Start_date or End_date is not a valid date, DATEDIF will return a #VALUE error.
  • It will also return a #NUM error if the ‘Interval’ is invalid.

Some Examples of DATEDIF function:

Start_date
End_date 
Formula
Description (Result)
15-04-2011
25-02-2014
=DATEDIF(Start_date,End_date,"Y")
Two complete years in the period (2)
06-08-2009
10-12-2014
=DATEDIF(Start_date,End_date,"D")
(1952) days between August 6, 2009, and December 10, 2014
03-03-2014
15-10-2014
=DATEDIF(Start_date,End_date,"MD")
The difference between 3 and 15 = (12) — the day of start_date and the day of end_date — ignoring the months and the years of the dates
01-06-2012
15-08-2013
=DATEDIF(Start_date,End_date,"YD")
(75) days between June 1 and August 15, ignoring the years of the dates.
15-04-2011
25-02-2014
=DATEDIF(Start_date,End_date,"M")
Number of months between April, 2011 and February 2014 — (34)

Formula to calculate your age in years, months and days.

Replace  12/01/1968 by your birth date and the following formula will return your age in Years, Months, Days:

=DATEDIF("12/01/1968",TODAY(),"y")&" years,"& DATEDIF("12/01/1968",TODAY(),"ym") &" month(s), "& DATEDIF("12/01/1968",TODAY(),"md")&" Days"

The need to supress zero values:

=IF(DATEDIF("12/01/1968",TODAY(),"y") = 0, "", DATEDIF("12/01/1968",TODAY(),"y") &" years,")& IF(DATEDIF("12/12/2012",TODAY(),"ym") = 0, "", DATEDIF("12/01/1968",TODAY(),"ym") &" month(s), ")& IF(DATEDIF("12/01/1968",TODAY(),"md") = 0,"",DATEDIF("12/01/1968",TODAY(),"md")&" Days")

Excel CEILING and FLOOR Functions



The Excel CEILING and FLOOR functions are very similar and are usualy used to round up or  down numbers based on a multiple of significance.


Syntax: 
  • CEILING( number, significance )
  • FLOOR( number, significance )

Parameter list: 
  • number - The number that you wish to round up (ceiling) or down (Floor).
  • significance  - The multiple of significance to use when rounding
Remarks:
  • CEILING always rounds up away from zero.
  • FLOOR always rounds down towards to zero.
  • If the significance argument does not have the same arithmetic sign as the number, both CEILING amd FLOOR functions will return the #NUM! error. 

Some Excel FLOOR and CEILING examples:

CEILING and FLOOR Excel Functions

Excel INDIRECT Function

The INDIRECT function returns the reference specified by a text string. You can use INDIRECT when you want to change the reference to a cell within a formula without changing the formula itself. You can use this function to create a reference that won't change in case rows or columns are inserted in the worksheet.

Syntax -  INDIRECT(ref_text,a1)

INDIRECT Function Arguments

As shown in the syntax this function has two arguments:

1 - Ref_text : is a reference to a cell that contains an A1-style reference, a R1C1-style reference, a name defined as a reference, or a reference to a cell as a text string.

2 - a1 : TRUE or FALSE.
            - TRUE or ommited, if you are using A1-style reference.
            - FALSE, if ref_text is an R1C1-style reference.

Important: If ref_text refers to another workbook, the other workbook must be open, otherwise INDIRECT returns the #REF! error value.

Using INDIRECT to lock a cell reference

I'll use an example to demonstrate how it works, but using two formulas.
  1. In cell A2, type a number (e.g. 500)
  2. In cell B5, type this formula:   =INDIRECT("A2")
  3. Press the Enter key and the formula will return the content of cell A2. (in this case, will be the number 500)
  4. Now on cell B6 type in the formula: =A2 and press Enter. Result will be 500.
  5. To confirm that the cell is locked... add a new line at the top of the worksheet.
  6. As cell B6 (B5+ 1 added line) uses the INDIRECT function, now returns 0. Formula has locked cell A2!
  7. Cell B7(B6+ 1 added line) which contains the link still returns 500 because the formula has changes to A3
Now with a picture:
Using INDIRECT to lock a cell reference
Cell reference locked with Indirect function

As you can see on the right side, after adding one line the cell reference in the Indirect function remains unchanged.

Indirect within a Vlookup

This can be an explosive combination of excel functions.

Here is one good example. Imagine that you have an excel file with several worksheets where each one of them contain the sales made in different cities. Then you have one more sheet - overview - where you want to be able to visualize sales of a certain city based on your selection.

Indirect within a Vlookup


On cell B4 of the picture above you can type/select the city name (it must match one of the sheets names). In this case I've selected NewYork on cell B4. The formulas used on cell C5 and cell D5 will return the "Total" sales available in the second Sheet of this workbook named "NewYork".

Cell C5  -   =VLOOKUP("Total",NewYork!$A$1:$D$15,2,0)
Formula explained:
1 - Search "Total"
2 - On a sheet named NewYork - Range $A$1:$D$15
3 - Retrieve data from column 2 of the selected range (will return data from collumn B on Sheet      
     NewYork if Total is found on collumn A)
4 - Exact match - 0 

Cell D5  -   =VLOOKUP("Total",INDIRECT("'"&B4&"'!A1:B30"),2,0)
Formula explained:
1 - Search "Total"
2 - On sheet that has a name equal to cell B4 - NewYork and on Range $A$1:$B$30
3 - Retrieve data from column 2 of the selected range (will return data from collumn B on Sheet      
     NewYork if Total is found on collumn A)
4 - Exact match - 0 


Do not forget the single quotes - INDIRECT("'"&B4&"'!A1:B30")

Now, I'll select Toronto instead of NewYork and the result is:

Indirect within a Vlookup

As you can see in this picture, cell C4 remains with the same value while cell D4 has changed to "208". Why ?
When I've changed the city name from NewYork to Toronto, the formula on cell C4 remained unchanged and is still retrieving the Total from Sheet Named "NewYork". But the formula on cell D4, has adapted itself  (INDIRECT("'"&B4&"'!A1:B30")) according the new content of cell B4 and is now retriveing data from Sheet Name Toronto.

Create a Reference To a Different Sheet or Workbook

In case you need to refer to cells on other worksheets INDIRECT formula can help.

Here are some examples:

Refer to cell A4 on a sheet named according to the value on cell A10

1 - Open a new workbook, add a second sheet called Data and a third sheet called Overview
2 - On the first sheet, on cell A10 enter the text Data
3 - On cell D5 on the same worksheet enter this formula: =INDIRECT("'" & A10 & "'!A4")
4 - Press Enter (Case 1)
5 - As a result cell D5 will return the content of cell A4 on worksheet Data
6 - Change the content of cell A5 from Data to Overview and press Enter (Case 2)
7 - The result of cell D5 will now be the content of cell A4 on worksheet Overview

Create a Reference To a Different Sheet or Workbook

If you delete the content of cell A10 on the first sheet of your workbook, the above mentioned formula will return an error. To prevent this, you can add an IF function:
=IF(A10="","",INDIRECT("'" & A10 & "'!A4"))


Refer to a the Cell mentioned in cell B2 value on a sheet named as the value on cell A10

In this example both the reference cell as well as the worksheet name are available in two diferent cells of your current worksheet.

The formula that you should use is:  =INDIRECT("'" & A10 & "'!" & B2)

Cell A10 contains the worksheet number and cell B2 contains the cell address that you want to refer to.
Create a Reference To a Different Sheet or Workbook

If you delete the content of cell A10 or B2 on the first sheet of your workbook, the above mentioned formula will return an error. To prevent this, you can add an IF function:
=IF(OR(A10="",B2=""),"",INDIRECT("'" & A10 & "'!" & B2))


Excel countdown timer until a date

Do you need to add a timer into one of your excel files to countdown the number of days until a certain date or deadline ?
Just place this formula in the cell you want the value to be displayed. This example will show the number of days between today and the end of 2014.

=DATEVALUE("31-12-2014")-TODAY()&" days remaining in 2014"

Formula explained:
DATEVALUE(text) is a excel function that converts a date to a number that can be used in calculations.
TODAY() is a function that returns the current date as a number using the computer's system clock.

This part of the formula =DATEVALUE("31-12-2014")-TODAY() subtracts the number for today from the number for your future date, resulting in the number of days between the two dates.
&" days remaining in 2014" displays text in front of the number retunred by the formula.

Double click formula to follow link?

Some Excel users often link several workbooks by using formulas that references a cell in another file.
When there is a need to follow the formulas arround, the least experienced users will open the several linked workbooks manually but most of the medium/high experienced users will just use the double click function.
So, what's this double click function I am talking about ?

Imagine the following scenario:

  • You are working on Workbook_A which Sheet1/Cell A10 uses a value from Workbook_B/Sheet1/Cell C20; 
  • You are working on Workbook_A and Workbook_B is closed;
  • If you double click Workbook_A/Sheet1/Cell A10 with your mouse left button it will open the formula for editing! So, how can you open the source file (Workbook_B) with a double click on Workbook_A/Sheet1/Cell A10 ?
  • In case you are using Excel 2010 or higher go to File > Options > Advanced > Untick the option for "Allow editing directly in cells"
  • Now, go to Workbook_A/Sheet1/Cell A10 and double click it with your left mouse button!
  • There you go! Workbook_B is now open and Sheet1/Cell C20 is selected.

PROS:
This excel "trick" can be quite helpfull for those Excel users that usually work with several linked workbooks because it will save all that time that would be needed to search for the source file, open it and finally find the right cell.

CONS:
Well, there are two cons for this "trick":
  1. While the "Allow editing directly in cells" option is unticked you won't be able to edit the cell just by double clicking it. You have to use the F2 button to edit cells.
  2. If you have a comment as well as a formula, it will open the comment instead! You can overcome this CON with a "floating" comment using Data Validation.



DGET Function - Lookup data in excel with multiple criteria

Retrieving data from a table/range without any limitations is a dream for almost every excel user. VLOOKUP is a very powerful function but unfortunately it doesn't allow us to lookup values in the left columns. Further more, with VLOOKUP we can only retrieve data considering one criteria...which might not be enough in some cases.

There is of course the possibility of combining functions to overcome situations mentioned above but for all those who prefer to use simple formulas,  Excel provides the DGET function.

How to use the DGET function:
=DGET(database, field, criteria)

Here are a couple of examples:


How to use the DGET function


Example #1 - Two search criterias(Item and Rep) that will retrieve a value from a column on the RIGHT.
There is only one row where Jones has sold pencils (row #3). The formula used for this example will return a value from column number 5 (Column E), as requested in the second parameter of our formula (field)

Example #2 - Three criterias (Rep, Item and Units) that will retrieve a value from a column on the LEFT.
In this example, there is also only one row on the table where Jones as sold 95 Pencils. The formula used in this example will return a value from row #3 and column number 2 (Column B), as requested in the formula.

Did you receive an error message?
This function can return one of the following two errors:
#NUM! - This message is displayed if the DGET function finds more than one record that meets the search criteria.

#VALUE! - This message is shown in case no records were found matching the search criteria.

PROS:
 - Retrieve data from a column to the right or left;
 - Lookup data based on multiple conditions;
 - Warning about multiple records with the same search critera.

CONS:
 - Inability of using auto fill the formula to several rows.


By the way, DGET function is also NOT case sensitive.

Video example from YouTube:

Excel VLOOKUP Function explained

VLOOKUP is one of the most powerful and useful functions of Excel. Anyone that masters with this function will have their work incredibly easier.

So, how to use the VLOOKUP function ?

Using the VLOOKUP function to retrieve value from columns on the right is as easy as placing this formula in the cell where you want to have the result:  =VLOOKUP(lookup_value, table_array, col_index_num, [range_lookup])

lookup_value - Value to be found in the first column of the table_array that you will specify.
table_array  - Table from where you want to retrieve a result based on your lookup_value
col_index_num  - Column number from the table_array from which the matching value should be returned.
[range_lookup]  - Is a logical value: to find the closest match = TRUE (sorted in ascending order); To find an exact match = FALSE

As an example, check this video:




Cons of VLOOKUP
 - In case the table_array contains more that one lookup_value, VLOOKUP will retrieve the first value found, ignoring the following ones.
 - VLOOKUP by itself can only be used to retrieve values from columns on the right. This mean that you cannot just enter (-1) on col_index_num to retrieve values from the column on the left side.

Solutions for theses two CONS will be published soon. Stay in touch.

How to prevent duplicate entries in excel using Data Validation?

One of these days I had task where needed to build a matrix with quite a lot diferent product codes.
Of course, the remove duplicates function would have been a great work arround but these product codes where going to be keyed in one by one in diferent times and several people. So, I've found a way to add come conditions to the pre-designed matrix. To do so, I've used Data Validation funtion.
So, here is a quick and easy way to prevent duplicate entires in any table that you need to build in excel:

  1. You need to select your data range where you want the prevente entry of duplicates.
  2. After selecting the range, go to Data Tab and click on Data Validation
  3. The Data Validation Window will show. In the Settings Tab select Custom  and enter the following formula: =COUNTIF(range,criteria)<=1


Prevent duplicate entries in excel using Data Validation
To avoid duplicates in the table shown in this example, you'd have to enter the fórmula: = COUNTIF($A$1:$F$10,A1)<=1

Explaining the formula: =COUNTIF(range,criteria)<=1

The COUNTIF function will check the number of occurences in the defined range(including the value that you are keying in) is less than or equal to 1. If so, that means that no other cell in the range contains a value equal to the one you're keying in. In case this function returns a value higher than one, that means the you're trying to enter a duplicate value and in this case you'll get the following error:

Prevent duplicate entries in excel using Data Validation
As the value "10055" in already exiting in Cell A1, excel won't allow the user to enter the same value in cell B3.

And there you go!
Your table is now ready to prevent the entry of duplicate values.



Highlighting alternative rows or columns in tables

Highlighting alternative rows or columns in tables
From time to time we have to present data in large tables and it might not be easy to read if the background color is the same for all rows or columns. Excel conditional formatting can be a tremendous help to make data tables less monotonous by changing background colors of alternative rows or columns. Of course you can paint the rows or columns yourself, but what about it you need to add a column or a row in the between?
Well, the trick to overcome this problem lies in Conditional Formatting.

And here is how to do it:
  • The 1st first step is to select the part of the table that you want to format.
  • Go to Conditional formatting dialog (Home Tab > Conditional Formatting > New Rule).
  • Select the last line "Use a formula to determine which cells to format".
  • Now, if you want to highlight alternative columns, enter the following formula, =MOD(COLUMN();2)=1
    which means, whenever row() of the current cell is even, to change the coloring to odd rows, you just need to put =MOD(COLUMN();2)=0 as formula
  • Click the "Format" button to format the columns as you like (I have used green color). The final result should look like this:
Highlighting alternative rows or columns in tables
  • Click OK.
  • Job Done

There are endless possibilites:
  • In case you want to highlight alternative rows instead of columns, just use row() instead of column() formula.
  • Now, lets imagine that you want to have switch background colors of every 4rd collumn instead, just use =MOD(COLUMN();4)=1. Your imagination is the limit.

Convert negative numbers with the sign on the right


I've previously explained how to convert text data into their numerical values. Now I'll show how to convert negative numbers (as text) and with the negative sign (-) to the right of the number. This might be useful especially when importing data from some accounting programs.

Convert negative numbers with the sign on the right


For example, suppose you find the text value 100- in cell D2. To convert that value to a negative number, enter this formula in a blank cell: =LEFT(D2;LEN(D2)-1)*-1.

*Note that you might have to replace the ; by , in the formula depending of your location.

Excel - Convert text values to numbers with Paste Special

Usually when we copy numerical data from other sources (e.g. pdf file, internet, Word, Access) to Excel, it behaves as text. Doing calculations with this "text" data can be a quite challenging but fortunatelly there is work arround to overcome this problem that is - Convert those values from their native text versions to more workable number fields in Excel.

Here's how to do it using Excel's Paste Special feature.
Follow these steps

1. Click a blank cell formatted as a number cell and enter 1 in the cell.

Convert text values to numbers with Paste Special2. Click Ctrl + C   (Copy)

3. Select all the cells that contain the text values that you want to convert.

4. Click Ctrl + Alt + V  (Display the Past Special dialog box)

5. In the Operation section, select Multiply.

6. Click OK.

There you have. Text values quick converted into their numerical values.