Pointy Haired Dilbert - Chandoo.org

Pointy Haired Dilbert - Chandoo.org

Excel Tips, Technology Tidbits and Business Insights

extract-hyperlinks-from-cell-excel-spreadsheet-formulaOften my work involves processing web page data in excel sheets. This includes extracting the hyperlinks from cell contents. There is no formula for extracting hyperlinks though, you can right click on cell and choose “edit hyperlink” to see which address the cell is linking to. But that is a tedious process especially if you are planning on using the hyperlink for something.

Here is a handy user defined function in VBA for getting hyperlinks from a spreadsheet cell:


Function getURL(forThisCell As Range) As String
'VBA UDF for getting URLs from a cell if any
retVal = ""
If forThisCell.Hyperlinks(1).Address <> “” Then
retVal = forThisCell.Hyperlinks(1).Address
End If
getURL = retVal
End Function

Bonus tip: You can create hyperlink on a cell using “hyperlink()” spreadsheet function. The syntax is simple. =hyperlink("http://chandoo.org/wp","Pointy Haired Dilbert") will create a link in the cell to this blog.

generate-bingo-tickets-in-excelI am fascinated by board games. They provide immense fun, anyone can enjoy them, they are unpredictable and best of all they are great value for money. That is why whenever I get sometime I experiment with simulating games to know them better [read Why Monopoly board game is not as random as it appears]. So, out of curiosity I have created an excel sheet that can generate bingo / housie (housey) tickets - 24 of them at a time. To get new set of tickets you would hit F9 (recalculate).

download bingo / housie / housey card maker excel sheet  Download Bingo / Housie ticket maker excel sheet

Note that these are Bingo UK / India / Australia variant I am talking about, not the US 5*5 type of bingo tickets.

Read on if you want to know how this is done:

According to Wikipedia:

A typical housie/bingo ticket .., contains fifteen numbers, arranged in nine columns by three rows. Each row contains five numbers and four blank spaces. Each column contains either one, two, or very rarely three, numbers:

* The first column contains numbers from 1 to 9,
* The second column numbers from 10 to 19,
* The third 20 to 29 and so on up until the last column, which contains numbers from 80 to 90.

I have removed number “90″ from the list in order to reduce some complexity in generating the tickets.

The problem is now to “generate 15 random number between 1 to 89 and fill them in 15 random spots in a grid of 3 rows by 9 columns such that each row has exactly 5 numbers”

Now I could write a function in VBA to do this, but I wanted to do this only using formulas. So I started breaking the problem.

The first challenge is to select any random 5 cells in a 9 cell row

Once we select any random 5 cells in a 9 cell row, we will fill them with bingo numbers. Now, excel has a function to generate random numbers between 1 to 9 (=round(rand()*9,0)), but this is not good for us since each time we call this function we will get a random number between 1 to 9, where as we need a 5 random numbers without repetition between 1 to 9. The function is memoryless and could repeat numbers when called 5 times.

Instead we can list all the possible “5 cells with numbers and others are empty” combinations of a 9 cells region and select a random combination every time. There are essentially 9C5 i.e. 126 ways in which you can select any 5 cells out of 9 cells (without repetition of course).

So I listed all these combinations in a table and then randomly selected one of the combinations. You can see the first five such combinations in the image below:

Selecting any five cells out of nine cells

Selecting any five cells out of nine cells

Now I created a 3*9 region and filled the cells with 1s or 0s, “1″ when the cell in bingo ticket is supposed to have a value and “0″ if the cell is empty as shown below:

raw-3-by-9-bingo-cell-grid-in-excel

Next challenge is to show random values in each cell

The trick here is that first column in our 3*9 bingo ticket has any number(s) from 1 to 9, second column has any number(s) from 10 to 19 …

Again, the challenge is the numbers should not repeat, otherwise we could simply use rand()*10, rand()*10+10, rand()*10+20 ... to generate the numbers.

This time it gets even more trickier because each column can have either no values, or 1 value or 2 values or 3 values.

The ticket generation logic now looks like:

  • If the column has no values in it, then we will leave all the cells in that column of bingo ticket empty
  • If the column has 1 value, we will generate any random number from that column’s range of possible values (1-9, 10-19,20-29,…80-89) and place it in the cell that is supposed to have a value and leave other cells empty.
  • If the column has 2 values, we will generate 2 random numbers without repetition from that column’s range of possible values and place them in cells that are supposed to have them
  • If the column has 3 values, we will generate 3 random numbers without repetition from that column’s range of possible values and place them in cells that are supposed to have them

As you can see, it is easy when the column has no or 1 value in it. But when the column has 2 or 3 I used the combinations trick described earlier.

First I created all 2 number combinations and 3 number combinations. Since the numbers on Bingo ticket are always sorted from top to bottom in a column, I just had to list down 45 combinations (10C2) for 2 numbers and 120 combinations (10C3) for 3 numbers.

The rest of the details are small enough that I can leave them to your imagination. So when the ticket is generated, it looks like this:

final-bingo-housey-ticket-printed-using-excel

Remember to download housie / bingo ticket generator excel sheet and print your tickets at home. Just F9 to generate new set of tickets. Un-hide the rows from 43 if you want to see how this is done.

Like this post: Consider digging it or bookmarking it on del.icio.us or better still subscribing to my feed, its yummy :D

Formatting numbers in excel - few tips

In: excel, hacks, technology

Reader Nikhil Shah asks in an email:

Dear Chandoo,

I have some Error In Excel sheet.

Problem :-

I have a locker number 01234 567890, in Excel it will be displayed as 01234 567890 with the space. However if it was entered as 01234567890, Excel will display it as 1234567890, without the leading zero. If the data has been read in from a text fill in could be formatted as text and still show 01234567890.

Now I want my locker number see with zero in excel sheet,How it possible,please help me with example.

You can force excel to format numbers the way you want using “format cell” feature. Just select the cell with number you want to format and hit ctrl+1 (or right mouse click > format cells) and go to Number tab in the dialog box. Select “custom” category and enter format as 000000 00000 (6 zeros followed by a space and then five more zeros) as shown below. Hit ok to set the format to display locker number as you desire.

excel-number-custom-formatting

Also, try these things:

  • To set social security number (SSN) format, enter 000-00-0000
  • To set phone number format, enter 000-000-0000
  • To set phone number with 1 digit country code format, enter (+0) 000 000 0000, for 2 digit country codes you can try (+00) 000 000 0000.
    • To set social security number (SSN) format, enter 000-00-0000
    • To set ZIP code format, enter 00000, for 9 digit ZIP codes you can try 00000-0000

    Do read Using Custom Cell Formats in Excel - Tips & Tricks to findout how to format dates, currencies, special formats etc.

excel-mutiple-windows-howto

Often we work with large data sheets and it becomes tedious if you want to compare data / formulas etc. You can try split (menu > window > split or alt+w+s) to break the sheet in to two. But there is another cool thing you can do, open a new window for the spreadsheet. Just press Alt + w + n (menu > window > new window) to open the same spreadsheet in 2 windows. Any changes you make in one window are immediately visible in another window(s).

Bonus tip: you can do the same thing with WORD and POWER POINT as well, cool eh?

Excel base mutual fund portfolio tracker

Would you like to spend next 5 minutes learning how to create an excel sheet to track your mutual fund portfolio?


click here to download mutual fund portfolio tracker excel sheet I have created and play with it.

NOTE: I have updated the sheet to fix a formula error, download it again if you need to.

We will use 2 simple excel features to achieve this - web queries and vlookup()
[click here to learn more about web queries in excel]

  1. First, lets put a tabular format for our portfolio: We can have fund name, # of units, purchase NAV (Net Asset Value, the cost of unit for your when you bought it), purchase date, total value at purchase (units * purchase NAV), current NAV (we will pull this data from internet), value as of now (units * current NAV), Profit / loss amount and profit / loss % as our table columns. Once you learn how to do this, you can add more columns depending on what / how you want to track your MF portfolio.

    When you finish creating the table, it would look something like this:
    excel spreadsheet mf portfolio tracker table format

  2. Next, we will use web-queries to load the fund-names and the corresponding latest NAVs in a separate sheet. I have queried Association of Mutual Funds India [AMFI] - Latest Mutual fund NAV page since all my investments are in India. If you are in US or some other country you can query corresponding fund house / financial info aggregator sites (like google finance) to get the data. Remember to set “Refresh data on file open” on to get fresh data whenever you open the your tracker excel sheet.

    Since AMFI returns data in a text file with ; as delimiter, I had to parse the fund names and navs out of it using a combination of search(), left() and mid(). I will not get in to the details of how its done since you may have to process your data differently depending on source.

    Finally when the processing is done, we will have a table in the second sheet with all fund names and latest navs.

  3. Now, all we have to do is create lookup formulas (well just vlookup()) to get the latest NAV to our tracker table based on the entered fund name.
    • Assuming the fund name in which you invested is in cell “c1″,
    • Assuming the fund data is in table “sheet1!c1:d6000″ with “column c” containing the fund name and “column d” containing latest NAV,

    The formula for latest NAV can look like this:

    =vlookup(c1,sheet1!c1:d6000,2,false)

    Remember to use false for last parameter since fund names may not be sorted in alphabetical order on your source web page.

    Now we will repeat this formula for all the rows in latest nav column. I have built my portfolio tracker to track 20 funds at a time. Also, you can simplify formulas using named ranges.

  4. Finally we will write formulas for,

    current value = latest nav * units held
    profit/loss = current value - purchase value
    profit/loss % = “profit/loss” / purchase value

    You can add some conditional formatting to beautify the table (like turning text blue for profits and red for losses etc.)

  5. Thats all, you have now created a real-time mf portfolio tracker. It would look something like this when done:
    mutualfund portfolio tracker excel sheet
    You can do the same for stock portfolios, commodities etc. You just need a web source that gives you latest data and five minutes of free time

Feel free to download mutual fund portfolio tracker excel sheet I have created and play with it.

Few ideas on how you can enhance this:

  • Add graphs to see visually how the funds are doing
  • Build some VBA to store previous NAV values of your funds so that you can see historical dates
  • Instead of doing plain % of profit / loss, compute realistic growth of your funds using date of purchase, risk free rate of return etc.

Here is a ridiculously simple workaround for those of you trying to generate an organization chart in excel:

Use google org. chart widget instead :)

[If you are reading this in a feedreader this post may not display properly, visit the post page instead.]

Open a new google docs spreadsheet (or work on the example organization chart spreadsheet I have created) and enter your organization employee data in the format shown below: (enter employee name in column 1 and manager name in column 2)

Create a organization chart gadget (menu > gadget > organization chart “add to sheet”), once done, the org chart should look something like this:

Its very easy to do and saves you a lot of time. If you need some example you can access the organization chart spreadsheet I have created.

how to get more colors in excel charts, beating excel chart color limitation - how to
Here is a simple to trick to beat the 56 color limitation in excel when you are designing a chart: use picture files (jpg, gif, png etc.) to fill the chart area.

You will no longer have to worry about limiting your project report / website / annual report etc. colors to the 56 that excel has.

1 + 3 steps to get more colors in excel charts:

  1. First create a 1*1 pixel sized image of the color you want to use in excel. You can use any software like MS Paintbrush to do this, all you need is the hex code or rgb of the color you want. Visit colour lovers if you are looking for cool color ideas.
  2. Right click on any of your chart data points and select format data point
    excel tip to get more colors in a chart
  3. In the dialog select “fill effects”
    how do I get more colors in microsoft excel spreadsheet?
  4. Finally, go to “Picture” tab in fill effects and specify your picture file path.
    how to enable more than 56 colors in ms excel
    OK your changes. Thats all, now your charts have any color your want.

Also: Download 73 beautiful excel chart templates | Art of excel charting | Become a conditional formatting pro