Code

XL Dev Con – Portable Code – Mike Staunton

Dr Mike Staunton author of such modern classics as Advanced Modelling in Finance Using Excel and VBA (The Wiley Finance Series) and Advanced Modelling in Derivatives Using VBA and C++/CLI (The Wiley Finance Series) and Triumph of the Optimists: 101 Years of Global Investment Returns and a few other ones as well, gave a entertaining talk about making code portable.

I just save my code to a USB, and that’s as portable as it get, but that not what Mike was talking about!! ;-) [Hilarious that one Ross!!!]

Mikes suggestion, or if not a direct suggestion then at least the offer of a alternative method, should portability be of interest was to limit the typing conventions, and to avoid where possible the use of function structures or orators  inherent to language type – for example Square Root can be taken as SQRT(A ), or A^int(b) and so on.

Dr Mike Sutton, don't mind your language

After this its a case of editing the file, what Mike referred to as Find and Replace, but what programmers call re-factoring, because their cleaver like that!

The benefit of of all of this was a speed up in the running of the code “for free”, by allowing it to be implemented in a different language, in Mikes case XL DNA, but also as he pointed it at this point it not that far away from C++ if that floats you pointer…

Thanks Mike, who’s next?

Revision Number Stops Addin Loading

Yesterday I deployed a Addin to a handful of users.

These days I mainly deploy my .Xla/m using a mixture of  Inno Setup and a .net app to automate the Excel OM, and use that to add the addin to the addins list. One way or another I have found this the most robust/easy method.

Today I wanted to issue a “HotFix” for the addin – such is my commitment to continues improvement you understand – but I hit a problem.

My plan was to write (modify) the Inno script so that an updated version of the .Xlam was overwritten into the same Dir with the same file name that the original set up had created.

This worked no problem, but when I opened Excel the Addin was not loaded. Attempting to reload the addin via the addins diolog lead to some sort of error message saying that the file had unreadable content or something… I was at a lost as to what the issue was.

After a while trying different things, I hit across the idea that I had also change some of the data in the File Properties. The most likely looking one to cause an issue was the Revision number. I had changed it form 1, to 2.0

I is long, or I might be Int

I changed it to 2, and tried the process again. Bingo.

So it seems that chaning that number to have any sort of decimal place in it can cuase a problem – there you go, tuck that away in the back of the head for future reference!

OCX dispaly issue, a simple fix

Firstly hello, its been a long time.

Now with that gushing show of emotion out of the way….

I’ve been working with an OCX componet that displays shapefiles (map files). It works nicely apart from the fact that when the form loads, the laoded shapfile does not display, not untill the form is resized, or the control is zoomed, or some other window moves over the controls window etc. Clearly there is a “repaint” issue some where.

Wheres my dam Map!

After trying lots and lots of me.repaint, me.map1.zoom, me.hide/me.show type of work arounds and getting nowhere,  I fianlly remebered in the resizer form by Steven Bullen, he used a lable to force a repaint, or somthing along thoses lines, so I gave that a whirl….

Adding a small lable onto of the OCX

Well what would you know!!

Bingo!!!

So there you go, if you have a misbehaving OCX, or any sort of form repaint issue try adding a lable on top of the control!!
See you in 6 months ;-)

How to Use an Excel Formula from VBA

This post is by guest blogger Yoav Eze, his bio can be fond at the bottom of the post. Thanks Yoav!
It’s always nice to get content from other Excel developers, so if you have an interesting story, please feel free to share it here!!! Get in touch: rossmcleanatgoogledotcom
That now makes a list of 2 guest bloggers on MIE in 5 years, Sam and Yoav, I’m hoping to grow that list!!!
Now, over to Yoav.
Excel has a powerful set of formulas, and Visual Basic is a capable language, but have you ever wondered if you can use the Excel formulas within your VBA macros?
There is a way! In fact there are at least two ways to go about this, each option is good for a different scenario.
.
Worksheet Function
If you want to use a standard function within your VBA code, and you don’t need to allow the sheet’s user to change it in anyway (i.e., it’s in the code and shouldn’t be changed), you can use the Excel’s WorksheetFunction object. This object contains all Excel’s functions as member functions, so you can use them directly.
For example:
Application.WorksheetFunction.Sum(TheRange)
This will return the sum of the cells in the range defined by ‘TheRange’.
When a range is required within a worksheetfunction function you will need to use a VBA object.
So this function won’t work:
Application.WorksheetFunction.Sum(A1:A10)
But this will work prefectly:
Application.WorksheetFunction.Sum(Range("A1:A10"))
Additionally, when you type the period after WorksheetFunction in the VBA editor, Excel’s intellisense will display the list of all the functions as they are the members of that object. You can also use the editor’s Object Browser (available via the F2 key or View -> Object Browser) to view those functions, including the arguments they get and the return values types.
.
Evaluate
Here’s where it gets really cool…
Another way to go is to use the Excel’s worksheet or application object’s Evaluate function. This function takes a string and evaluates it, returning either the value it refers to (if it’s a formula) or the range it refers to (if it is a cell or range reference in A1 style).
You can also use square brackets – [] – instead of evaluate.
.
Examples:
Me.Evaluate(“A1″)
will return a reference to cell A1
.
Evaluate("Sum(A1:B3)")
will return the sum of the range A1 to B3
.
[A1].Font.Bold = True
will set font of the A1 cells of the current worksheet to be bold
.
You can combine loops and formulas this way:
For I = 2 To 8
Debug.Print Evaluate("A" & I), Evaluate("countif(A1:A" & (I - 1) & ",A" & I & ")")
Next I
This will print for each cell in range A1:A100 how many times the cell value has appeared before in the range, so for this sheet:
.
..
The output will be:
a1
b0
b1
c0
d0
f0
b2
You can make this code even cooler by using a formula to count how many values there are in column A, like so:
For I = 2 To [counta(A:A)]
Debug.Print Evaluate("A" & I), Evaluate("countif(A1:A" & (I - 1) & ",A" & I & ")")
Next I
Note that I used [counta(A:A)] to count how many cells have values in column A directly from within Visual Basic.
.
Summary
So there you have it! Using those notations, you can combine the flexibility and power of VBA with the ease of use and functionality of Excel Formulas. Can you think of anywhere you could use these features right now?
About the author
Yoav Ezer co-authors the technology and productivity blog Codswallop. He is also the CEO of a company that produces PDF to Excel conversion software.
For more Excel tips from Yoav, join him on Facebook or Twitter

The MIE Podcasts – Charles Williams

Charles Williams is probably best known for his work in Excel calculation speed and VBA performance, not to mention the worlds most famous Excel Addin Name Manager. In truth, he’s a expert in many areas of Excel and on top of all this he’s a great bloke. In this podcast we got down and dirty with Excel performance, uncovering some interesting insights along the way. It was a true pleasure talking with Charles.

Charles the First - King of Speed!

Charles in the founder of Decisions Models, a consulting firm specialising in Excel development, and the farther of the FastExcel addin, we talked about this addin and how it works and also his addin loader tool. This is one for the archives!

Thanks to Charles for making the time and sharing his knowledge with us all.