VBA and Conditional Formatting in Excel

Blog

Blog

Blog

Blog

all posts

tags

VBA and Conditional Formatting in Excel

by Rickard Warnelid on March 25 2009

The majority of the time at Corality we audit financial models or develop user friendly financial models for our clients. However, in a field that develops as quickly as financial modelling we need to make sure that we stay ahead of the competition - even in the long term.

Our method to constantly re-invent and develop our skill set is to experiment with Excel to develop new methods and applications that we had not earlier thought of. Every now and then we come up with something useful that can be integrated into our day to day practice of financial modelling. This time, I am not sure if that is what happened…

Using VBA to generate a cross-cursor

I presented a VBA training course in Sydney last week and one of the applications in that course is a dynamic cross-cursor for highlighting relevant data in a matrix format. (Here is an introduction to basic conditional formatting if you need one of those, and a bit more advanced here)

VBA cross-cursor with conditional formatting

VBA cross-cursor with conditional formatting

The VBA exercise shows the power of the worksheet functions and how it can be used to improve model presentation.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Generate a circle around the selected cell

After the VBA course I kept experimenting with the same concept, i.e. updating the worksheet based on the properties of the selected cell.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Description: Returns column and row of the selected cell
'Author: Rickard Warnelid - Corality
'When to run: Automatically returns column and row of selected cell

'Return selected column
[CursorColumn] = Target.Column

'Return selected row
[CursorRow] = Target.Row

End Sub

Convert the VBA trick into a ‘Find the hidden treasure’

This time I thought I would expand the conditional formatting to generate a circle around the selected cell by back-solving the equation c^2 = a^2 + b^2. This gives a circle with a fixed radius. The second step was to apply a scaling factor inversely related to the distance of the active cell and a defined Target Cell. The scaling factor was then used to determine the size of the highlighted circle around the active cell.

Excel conditional formatting calculations

Excel conditional formatting calculations

With “O17″ as the location of the ‘hidden treasure, the conditional formatting looks like this:

=SQRT((COLUMN(F20)-CursorColumn)^2+(ROW(F20)-CursorRow)^2)<$D$14

Graphical representation of combination of VBA and conditional formatting trick

Not even close...

Not even close...

Getting closer...

Getting closer...

Even closer....

Even closer....

Found it!

Found it!

I have been scratching my brain, but just can’t come up with a useful commercial application for this rather neat VBA trick. I suppose it could be used to really prove to your friends and colleagues that there is a real geek inside you, but that’s probably about it. None the less, it is a pretty cool feature given that it is generated with only two lines of VBA code.

Download the conditional formatting and VBA example here

If you can think of a useful application for this, please let me know!

COMMENTS

This is an quite interesting

This is an quite interesting application VBA and conditional formatting.

Post new comment

The content of this field is kept private and will not be shown publicly.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Need a financial model audit?

Need a financial model audit?

Do you want to find our more about our financial model audit services??

Read more about Corality

Upcoming Courses

Best Practice Project Finance Modelling
Sydney
30 May - 31 May 2012
Best Practice Project Finance Modelling
London
11 June - 12 June 2012
Advanced Project Finance Modelling
London
13 June - 14 June 2012

Bloggers

Laura Dean

Laura Dean

Laura is our creative and motivated Marketing Coordinator.

Rickard Warnelid

Rickard Warnelid

Rickard Warnelid is the Managing Director of Corality Financial Group.

Peter Weatherston

Peter Weatherston

Peter is a UK qualified accountant with 10+ years experience in financial modelling and financial model auditing.

Bing Chien Quek

Bing Chien Quek

Bing is Corality's in-house VBA expert. He has worked on numerous projects as well as facilitated VBA training courses internationally.

Caroline Wiroth

Caroline Wiroth

Caroline has over 8 years of experience in marketing and Public Relations.

Tim Heng

Tim Heng

Tim has a wide range of experience in analytics and modelling across a number of different industry sectors (corporate banking, vehicle finance, credit risk and marketing analytics).

Blake McNaughton

Blake McNaughton

Blake McNaughton is an Associate based in Corality’s Head Office in Sydney, Australia.

Haydn Palliser

Haydn Palliser

Haydn is an Associate at Corality with a wealth of experience in structured finance, consulting and is a chartered professional engineer.

Cathryn Fish

Cathryn Fish

Cathryn is our energetic and super organised Training Experience Manager.

Nick Crawley

Nick Crawley

Nick Crawley is the Chairman and Head of Consulting of Corality Financial Group.

Grace Utama

Grace Utama

Grace Utama is a financial modelling expert, with particular expertise in advanced Excel techniques and VBA Macros.

Have a question or comment? We would love to hear from you. More contact options

We respect your email privacy.

Sitemap | Terms | Privacy © Copyright 2012 Corality Financial Group