Excel VBA and Finance World

Trying to put in all the Tips related to Excel, VBA and Finance that I think can be useful for those who are in this field


We often come across situations where we need to create a line/column chart to see the performance of sales/share price etc. However expanding the chart series every time there is an update/new data to be added is always time consuming. Therefore if in some way we can make the series of chart data dynamic then it will also make the chart dynamic.
Let see an example: Suppose we have to create a line chart for monthly price for 3 stocks for last 1 year and every month there will be a new addition for the next 1 year. We can easily create it by selecting line chart from excel and having months on column A and share price of stock on columns B,C & D respectively. However if we select a long range to fit the data then wherever there is no data the chart will show blank/space left in the chart which is not a good way to construct, and if we don’t select a long range we have to manually add it every time.
Now let see how we can make the chart dynamic in such case.
Step 1: Create a data table as shown and save file as chat.xlsx




Step 2: Create a name range using Name Manager in Excel 2007 as shown below
MonthValues =OFFSET(StockAvalues,0,-1)
StockAvalues =OFFSET(Sheet1!$B$2,0,0,COUNTA(Sheet1!$B:$B)-1,1)
StockBValues =OFFSET(Sheet1!$B$2,0,0,COUNTA(Sheet1!$B:$B)-1,1)
StockCvalues =OFFSET(Sheet1!$D$2,0,0,COUNTA(Sheet1!$D:$D)-1,1)
Step 3: Now you can create a chart using the Chart wizard but make to have the series data in the following way:

=SERIES(Sheet1!$B$1,chart.xlsx!MonthValues,chart.xlsx!StockAvalues,1)
=SERIES(Sheet1!$C$1,chart.xlsx!MonthValues,chart.xlsx!StockBValues,2)
=SERIES(Sheet1!$D$1,chart.xlsx!MonthValues,chart.xlsx!StockCvalues,3)



And there you go…..Download the file from here:

Hi All, I came across a wonderful macro where you can change the formula of a give cell or selection if it gives you error values like #DIV!, #Value! etc. This is a very effective macro for files or excel models where you have to deal with lot of formulas and calculation that affects your output. Below are the steps for the file:


Save this file on your desktop and then open it


Open the model where you want to use the ISERROR formula


Highlight the range you want to use the formula


Hit Ctrl+e to run the macro for ISERROR formula


If you want to undo please use Ctrl+z


Please note this will work for only formulas where cell reference is done and not for the below:


#DIV/0! Text
50 Not a cell reference formula
#VALUE! comments

Below is the macro code attached in the file:

Type SaveRange
Value As Variant
Address As String
End Type


Public OldWorkbook As Workbook
Public OldSheet As Worksheet
Public OldSelection() As SaveRange




Sub If_Is_Error()
'
' If_Is_Error Macro
' Changes =x to =if(iserror(x),"",x)
'
' Keyboard Shortcut: Ctrl+e
'
Dim ManualCalc As Boolean


If Application.Calculation = xlCalculationManual Then ManualCalc = True
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False


Dim x As String


' Save current values for later undoing
ReDim OldSelection(Selection.Count)
Set OldWorkbook = ActiveWorkbook
Set OldSheet = ActiveSheet
i = 0
For Each cell In Selection
i = i + 1
OldSelection(i).Address = cell.Address
OldSelection(i).Value = cell.Formula
Next cell


' make each cell is_error="" if it is a formula
For i = 1 To UBound(OldSelection)
If Len(Range(OldSelection(i).Address).Formula) = 0 Then GoTo skip
If Left(Range(OldSelection(i).Address).Formula, 1) <> "=" Then GoTo skip
x = Right(Range(OldSelection(i).Address).Formula, Len(Range(OldSelection(i).Address).Formula) - 1)
x = "=IF(ISERROR(" & x & "),0," & x & ")"
Range(OldSelection(i).Address).Formula = x
x = 0
skip:
Next i


'Return claculation to automatic if required
If ManualCalc = True Then Application.Calculation = xlCalculationManual Else Application.Calculation = xlCalculationAutomatic
' If ManualCalc = True Then MsgBox "Calculation left on manual." & Chr(10) & "Press F9 to calculate sheet"


Application.ScreenUpdating = True


' Specify Undo
Application.OnUndo "Undo the IsError macro", "UndoIsError"


ManualCalc = 0


End Sub


Sub UndoIsError()


On Error GoTo Problem


Application.Calculation = xlCalculationManual
Application.ScreenUpdating = True


' Make sure the correct workbook and sheet are active
OldWorkbook.Activate
OldSheet.Activate


' Restore saved formulae
For i = 1 To UBound(OldSelection)
Range(OldSelection(i).Address).Formula = OldSelection(i).Value
Next i
Exit Sub


Problem:
MsgBox "Can't undo"
End Sub


Link for download:
http://www.esnips.com/doc/01ebaaa2-2b7d-4300-9fdc-ad01f06bb864/ISERROR-macro


We often come across situation where we have to select from a list of available options in excel. Let’s take an example different 5 regions for sales and for each region you have assigned numerical values from 1-5. Now you wish to just put any number 1-5 to get the sales for that region. And in this case your CHOOSE() function comes handy.
Lets first know how a choose function works:
The syntax of CHOOSE() function is CHOOSE(index_value, value1, value2….value5)
Here index_value is the position of the number in the list of values to return. In this case it is 1-5. A value can be any one of the following: a number, a cell reference, a defined name, a formula/function, or a text value. Therefore if you use a CHOOSE() function like:
CHOOSE(3,”Tea”,”Coffee”,”Hot Chocolate”). Then this would return Hot Chocolate.
Now back to our situation described above.
Step:1  Assign number from 1-5 to each region like
1 Asia Pacific
2 UK
3 US
4 Africa
5 Middle East
Step 2: You can now input any number from 1-5 in Cell D3 and use the 2 CHOOSE functions in any other cells as shown here CHOOSE($D$2,B3,B4,B5,B6,B7) and CHOOSE($D$2,C3,C4,C5,C6,C7) to get the desired result. Use the following attached sample file for your understanding.




How to avoid selection of range in excel that are grouped/hidden and only select the visible ranges.

We often come in situation where we have a range of data say quarterly reviews with annual reviews of sales. In such cases mostly the quarterly data is grouped/hidden to show that they are easily recognized separately from the annuals figures. However they is a problem when we want to copy and paste the annual data for some other calculation. In this case if we copy paste we would see the whole of quarterly and annual data selected together instead of only annual data. Therefore to avoid this we have a solution.

Step 1. Group all the quarterly data as shown below. Use ALT+D+G+G to group for a selected rage and then click on the + sign on the top of the sheet to hide the quarterly data.

Add Image

Step 2: Select the data you want to copy for annuals, in this case let’s say all of the data. After selection use the ALT+ Semicolon (;) on the key board. This will only highlight the selected ranges which are visible. Alternatively you can use Function Key F5, this will popup the Go To window. Then click on “Special” on the Go To window and you will see a list of radio button to select from. Use the “Visible cells only” and click OK.

Step 3: Now once you have the highlighted range you can copy paste which will only paste the annuals value.

Hi Guys,

Here is a list of websites which I am sure you all Excel VBA user like. Please share more websites if you think is useful. Please leave you comments as well.

1. http://www.exceltip.com/
2. http://www.mrexcel.com/articles.shtml
3. http://www.cpearson.com/Excel/Topic.aspx
4. http://groups.google.com/group/excel-macros/topics?hl=en&pli=1
5. http://xl.barasch.com/xlvbaa.htm
6. http://www.ozgrid.com/
7. http://www.vertex42.com/
8. http://www.familycomputerclub.com/
9. http://www.vbaexpress.com/portal.php
10. http://www.youtube.com/user/ExcelIsFun
11. http://www.functionx.com/vbaexcel/index.htm
12. http://www.xlmacros.com/

Hi Guys,

Below are the list of few Excel Books that you might find useful:

1) Learn Excel 97 through Excel 2007 From MrExcel - 377 Excel Mysteries Solved

2) Excel 2007: Beyond the Manual

3)
Charts and Graphs for Microsoft Office Excel 2007

4) Work with formulas and functions in Microsoft Office Excel 2007

5) Pivot Table Data Crunching for Microsoft Office Excel 2007

6) Excel 2007 Power Programming with VBA

7) Microsoft Excel 2003 Programming Inside Out

8) VBA and Macros for Microsoft Excel 2007

9) Wrox Excel 2007 VBA Programmer's Reference

10) Professional Excel Development: The Definitive Guide to Developing Applications Using Microsoft Excel and VBA

More to Come......

How to link quickly any cell reference in Excel sheets.

Here are the steps:

1) Copy the range you want to link to the other sheet.

2) Copy

3) Go to the destination sheet or cell/range where you want to create the link. Right Click and select Paste Special and then click on "Paste Link" option given below the popup window. ( or use ALT+ E + L)

Shortcut Action
= (+) Formula
Delete Clear Selected Text/object
Down Arrow Move Down
End End Row
F1 Help
F1 Help
F10 Activate Menu
F11 New Chart
F12 Save As
F2 Edit Mode
F3 Paste Name Formula
F4 Repeat Action
F5 Goto
F6 Next Pane
F7 Spell Check
F8 Extend Selection
F9 Calculate All
Home Begin Row
Insert Insert Mode
Left Arrow Move Left
Page Down Page Down
Page Up Page Up
Right Arrow Move Right
Space Bar Space
Tab Move Right
Up Arrow Move Up
Alt - Control Menu
Alt ' (apostrophe) Style
Alt ; (semicolon) Select Visible Cells
Alt = (+) Auto Sum
Alt D Data Menu
Alt Down Arrow Drop down list
Alt E Edit Menu
Alt F File Menu
Alt F1 Insert Chart Sheet
Alt F11 VB Editor
Alt F2 Save As
Alt F4 Quit Excel
Alt F6 Switch To VBA
Alt F8 Macro List
Alt H Help Menu
Alt I Insert Menu
Alt O Format Menu
Alt Page Down Right 1 screen
Alt Page Up Left 1 screen
Alt Space Bar Control Box
Alt T Tools Menu
Alt Tab Next Application
Alt W Window Menu
Shift Down Arrow Select Down
Shift F1 What's This Help
Shift F10 Context Menu
Shift F11 New Worksheet
Shift F12 Save
Shift F2 Edit Comment
Shift F3 Paste Function
Shift F4 Find Again
Shift F5 Find
Shift F6 Prev Pane
Shift F8 Add To Selection
Shift F9 Calculate Worksheet
Shift Left Arrow Select Left
Shift Right Arrow Select Right
Shift Space Bar Select Row
Shift Tab Move Left
Shift Up Arrow Select Up
Ctrl - Delete Selection
Ctrl / Select Array
Ctrl : (colon) Insert Time
Ctrl ; (semicolon) Insert Date
Ctrl [ Direct Dependents
Ctrl \ Select Differences
Ctrl ] All Dependents
Ctrl ` (~) Toggle Formula View
Ctrl 0 Hide Columns
Ctrl 1 (!) Cell Format
Ctrl 2 (@) Toggle Bold
Ctrl 3 (#) Toggle Italics
Ctrl 4 ($) Toggle Underline
Ctrl 5 (%) Toggle Strikethrough
Ctrl 8 (*) Outline
Ctrl 9 Hide Rows
Ctrl A Select All
Ctrl B Bold
Ctrl BackSpace Goto Active Cell
Ctrl C Copy
Ctrl D Fill Down
Ctrl Delete Delete To End Of Line
Ctrl Down Arrow Move Down Area
Ctrl End End Of Worksheet
Ctrl F Find
Ctrl F10 Restore Workbook
Ctrl F11 New Macro Sheet
Ctrl F12 Open
Ctrl F3 Define Name
Ctrl F4 Close Window
Ctrl F5 Restore Window Size
Ctrl F6 Next Workbook
Ctrl F7 Move Window
Ctrl F8 Resize Window
Ctrl F9 Minimize Workbook
Ctrl G Goto
Ctrl H Replace
Ctrl Home Start Of Worksheet
Ctrl I Italics
Ctrl Insert Copy
Ctrl K Insert Hyperlink
Ctrl Left Arrow Move Left Area
Ctrl N New Workbook
Ctrl O Open Workbook
Ctrl P Print
Ctrl Page Down Next Worksheet
Ctrl Page Up Previous Worksheet
Ctrl R Fill Right
Ctrl Right Arrow Move Right Area
Ctrl S Save
Ctrl Space Bar Select Column
Ctrl Tab Next Window
Ctrl U Underline
Ctrl Up Arrow Move Up Area
Ctrl V Paste
Ctrl W Close Workbook
Ctrl X Cut
Ctrl Y Repeat Active
Ctrl Z Undo
Ctrl + Shift - No Border
Ctrl + Shift ' (apostrophe) Copy Cell Value Above
Ctrl + Shift / Select Array
Ctrl + Shift ; (semicolon) Insert Time
Ctrl + Shift [ Direct Precedents
Ctrl + Shift \ Select Unequal Cells
Ctrl + Shift ] All Precedents
Ctrl + Shift ` (~) General Format
Ctrl + Shift = (+) Insert dialog
Ctrl + Shift 0 Unhide Columns
Ctrl + Shift 1 (!) Number Format
Ctrl + Shift 2 (@) Time Format
Ctrl + Shift 3 (#) Date Format
Ctrl + Shift 4 ($) Currency Format
Ctrl + Shift 5 (%) Percent Format
Ctrl + Shift 6 (^) Exponent Format
Ctrl + Shift 7 (&) Apply Border
Ctrl + Shift 8 (*) Select Region
Ctrl + Shift 9 Unhide Rows
Ctrl + Shift A Formula Arguments
Ctrl + Shift F Font Name
Ctrl + Shift F12 Print
Ctrl + Shift F3 Names From Labels
Ctrl + Shift F6 Prev Workbook
Ctrl + Shift O Select Comments
Ctrl + Shift P Font Size
Ctrl + Shift Space Bar Select All
Ctrl + Shift Tab Previous Window