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

Saturday, June 22, 2013

Generate Random Numbers in Excel

Do you feel need for inserting random/dummy numbers in excel cells?

Well, lot of times while making new reports or making new model in Excel I don't have actual data to work with. Sometimes back-end team is still working to get the data, sometimes it is dependent on someone else or sometimes you just have to create blank template and send it to business users who will enter the details and model should give the results based on the entered value by the end user etc.

Being the Excel users you all will know how difficult it is to check the formula without any values.

One can insert numbers manually in the cells. However, it will be very tedious task especially when you have lots of cells to fill.

Better alternative is to use formula. You can use below mentioned formulas to get the random numbers...

1. RAND
Syntex: =RAND()
It gives any random numbers from 0 to 1. One can use it with combination of ROUND function to get the rounded number up to specified decimals. Given below is the formula for getting random number from 0 to 1 having 2 decimals...
=ROUND(RAND(),2)

2. RANDBETWEEN
This function is similar to the RAND function, only difference is that you get the option of adding upper and lower limit within which the random numbers are returned by Excel.

Syntex: =RANDBETWEEN(lower limit, upper limit)

Example: =RANDBETWEEN(1000,10000) will return random number between 1000 to 10000

Note: Whenever there is change in any value of any cell, Excel recalculates values in all the cells having formula. This causes changes in the values having RAND or RANDBETWEEN formulas. If you don't want these values to get changed, remove the formula by pasting values immediately after getting random numbers.

Monday, June 10, 2013

Summing cells having Years and Months stored as years.month format in Excel

Yesterday, one of my friend asked me a question. It was quite simple, he had a data stored in specific way, and he wanted to do additions of few columns….

Question:
He had an Excel file, where data is stored in Years.Month format (i.e. 5 years 8 months is stored as 5.08) in two columns. He wanted to add these columns. Returned value should be in Years.Month format only. For e.g. 5.08 + 8.05 (i.e 5 Years and 8 Months + 8 years and 5 Months) the answer should be 14.01 (i.e. 14 Years and 1 Months). A simple addition formula in Excel will return 13.13 as Excel considers these values as normal numeric values.  Hence he wanted some way to do these additions.
I gave him the solution by writing below mentioned formula. Above mentioned situation may be common for many of us hence thought of sharing it here for others.

Solution:
We can achieve desired output by using below mentioned formula.  Assuming that 2 data items are stored in cell A2 and Cell B2, formula will be…
=IF(IF(MOD(A2,1)=0,0,VALUE(MID(A2&"0",FIND(".",A2)+1,2))) + IF(MOD(B2,1)=0,0,VALUE(MID(B2&"0",FIND(".",B2)+1,2)))>=12,
VALUE(1+INT(A2)+INT(B2) & "." & RIGHT(0&IF(MOD(A2,1)=0,0,VALUE(MID(A2&"0",FIND(".",A2)+1,2))) + IF(MOD(B2,1)=0,0,VALUE(MID(B2&"0",FIND(".",B2)+1,2))) - 12,2)),
VALUE(INT(A2)+INT(B2) & "." & RIGHT("0"&IF(MOD(A2,1)=0,0,VALUE(MID(A2&"0",FIND(".",A2)+1,2))) + IF(MOD(B2,1)=0,0,VALUE(MID(B2&"0",FIND(".",B2)+1,2))),2)))

Explanation:
Logic: We are adding Numeric portion (i.e. the years) and the Decimals portion (i.e. the months) separately. If summation of decimal portion is greater than 12 then 1 is added into numeric summation (i.e. 1 is added into the summation of years as 12 months is equivalent to 1 year) and at the same time decimal summation (i.e. summation of month) is reduced by 12.
Finally after addition, both numeric and decimal portion are concatenated with a dot (decimal sign) and shown as numerical value using the VALUE function.

IF(IF(MOD(A2,1)=0,0,VALUE(MID(A2&"0",FIND(".",A2)+1,2))): It returns decimal portion from the given value. E.g. for 5.05 it will return 5, for 6.10 it will return 10 and so on. If there is no decimal value then it returns 0. MOD is used to check if the given value has any decimal portion or not. If there is any decimal portion, MID function is used to take out only the decimal portion.

INT(A2): returns the Numeric portion of the given value. For e.g. for 5.05 it will return 5, for 6.10 it will return 6 and so on.

VALUE: function is used to convert text into values. All the operations are done by text functions and the final outcome is also a text. To convert this text into numeric value, Value function is used.

Tuesday, April 23, 2013

Vlookup Formula in Excel

Vlookup is one the most important and most widely used function amongst the available lookup functions in Excel. This is the first complicated formula that I have learnt after I started working.

Purpose:
It is used for searching corresponding value of a given text 
from a database . For e.g. Searching name, address, telephone number from  large customer database for few customer id. There are other alternative lookup formulas like match, offset, Index etc are available. But of all, vlookup is simplest and most widely used function.

There can be two main purposes for which we can use this function. 
  1. To append columns/fields in your existing data for additional information. For e.g. You have list of customers with id, names etc and you want to contact them using phone number which is available in another excel data file. You can add column for phone number in your data and use Vlookup formula to get the details for entire list



  2. For searching details like name, address etc for given text. E.g. Employee id, customer id etc. 




How to do Vlookup:
Lets take example of case 1 above. In this case, Customer phone number is taken using using vlookup function based on the "cust id" field from "Cust Basedata" worksheet. Lets call worksheet/table where you want the data as Destination (the worksheet/table where you need to insert vlookup formula) and the worksheet/table from where this information is available ("Cust Basedata" worksheet) as Source.



Vlookup function requires 4 parameters (separated by comma sign)...Each parameter is explained below. After explanation of each parameter, formula for first row of our example (i.e. cell C2 of destination) for respective parameter is mentioned in square  bracket in blue font. 
  1. Lookup_value: This the text for which you want to search corresponding value from another Excel database. In our example Customer ID in destination table is the Lookup_value. [A2]
  2. Table_array (range): It is the range in which your base data is present. In the example, range A1 to C31 of source is the table_array for vlookup. Note that your table_array should always start from the column in which the lookup value is present. i.e. the first column or the left most column of the table_array should contain the lookup value. In our example Column A is the left most column of the table_array and it contains the customer ids which we are searching for. ['Cust Base Data'!$A$1:$C$31]
  3. Col_index_num: This is the column number in the table_array from which the matching value is returned; starting from the left most column in the table_array. So, Col_index number 1 will return value from the 1st column, col_index_number 2 will return value from 2nd column and so on. In our example, column number for phone number field is 3, which we are trying to get in source. [3]
  4. Range_lookup (Lookup type): It's an Optional parameter, a logical value that specifies whether you want VLOOKUP to find an exact match or an approximate match. 
    • True or 1 (default if not specified): For approximate match. If exact match is not found, then it will return value for next largest value which is lower then the search value. Personally  I feel, this option has very limited use as most of the time you would want to do vlookup for exact match only. 
    • False or 0: Use this option to for searching exact match. vlookup will return #N/A error if it doesn't find the search value in table array. In our example we will use this option, since we are looking for phone number of customer id matching exactly. [False or 0]

Hence the formula for cell C2 will be =VLOOKUP(A2,'Cust Base Data'!$A$1:$C$31,3,FALSE). 
After applying drag/copy the same formula in the remaining list. i.e. cells C3 to C11 in destination table. 




Important points to remember while using vlookup:
  • Vlookup function will return #N/A error if the Range_lookup (4th parameter) is False and if it doesn't find the exact match of search value.
  • It will return #Ref! error when column_index (3rd parameter) supplied is greater than the number of columns in table_array. 
  • It will return #Value! error if column_index is less than 1.
  • It will return 0 if it finds the match and the corresponding cell for that searched value is blank. 
  • If the source has more than one values then vlookup function returns first value from the top most row. Hence it becomes very important to sort your source data when there are multiple records for the same value to ensure that the relevant rows comes above the other rows. For e.g. in our example, if there are more than one phone numbers for few customers with additional field of last updated date, then data can be sorted based on descending order of last updated date to get the latest updated phone numbers. 
  • Ensure that range you supply in Table_array parameter is absolute cell reference. (done by using $ sign before the column and row number)
  • Vlookup can be done only from left to right and not vice-varsa

Sunday, February 13, 2011

Post your questions on Excel

Hello,

If you have any doubts in excel, please post your questions on Help Forum. I will try and answer your queries.