VBA

XL Dev Con – Writing addins in C – Charles Williams

I was looking forward to this one. Charles has huge respect in the Excel commuitely, and has a list of achievement’s as long as I am tall. Charles is talking about how hes getting on with writing addins in C. This is proper C  not that C# rubbish!

Charles first makes the case for why he’s moved over to C, 64 bit and speed beign high on the list.  Next its in to VS2010 and Planatech XLL+ with a walk through of how to do it, and some of the gottas for a VBA to C convert. I’ve done a few Pure C XLL’s, using such tools a WXL it works well, but  XLL Plus sure looks nice, I like thoses wizards!!!

image

Synonymous with Speed Charles Williams at the helm

It was an insightful talk, not only for the how, but also for the, – it takes me longer, but is that because I have worked with VBA for 20 years? – I’ll ask if the speed gap has come down next year Charles!

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

Get a UNC path in Excel

Here’s some code taken from code project (i think) and exposed as a UDF, simple.

Download the workbook here: MIE UNC Path File Name UDF

How to use SHOWIF and SHOWUNLESS functions in Excel

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.

Have you ever in your spreadsheets wanted to show a value only under certain conditions?
We’ve created two functions show-functions (DOWNLAOD THE WORKBOOK HERE)  that will do exactly this. The first accepts a value (which can be a simple value, a cell reference, or a calculation) and a condition as a string (like the MATCH function). It then evaluates the condition on the value. If the condition is true, the value is returned, otherwise an empty string is returned. You can also specify a different return value if the condition is not met by setting a third parameter.

The second function does exactly the same, except that it will return the value if the condition is NOT met, and an empty string (or the third parameter) if the condition IS met.

Here’s an example of how you might use these functions. This is a formula used in one of our spreadsheets:

=IF((SUMIFS(Payments!$D$2:$D$1000,Payments!$C$2:$C$1000,"="&B$1,
OFFSET(Payments!$D$2:$D$1000,0,MATCH($A3,Payments!$E$1:$Z$1,0)),"x") -
 SUMIFS(Payments!$D$2:$D$1000,Payments!$C$2:$C$1000,"="&$A3,
OFFSET(Payments!$D$2:$D$1000,0,MATCH(B$1,Payments!$E$1:$Z$1,0)),"x"))<=0, "",
SUMIFS(Payments!$D$2:$D$1000,Payments!$C$2:$C$1000,"="&B$1,
OFFSET(Payments!$D$2:$D$1000,0,MATCH($A3,Payments!$E$1:$Z$1,0)),"x") -
SUMIFS(Payments!$D$2:$D$1000,Payments!$C$2:$C$1000,"="&$A3,
OFFSET(Payments!$D$2:$D$1000,0,MATCH(B$1,Payments!$E$1:$Z$1,0)),"x"))

Looking closely, it’s possible to see that we have the same formula appear twice; once it is checked to see if it’s less then or equal to zero, and again as the result.

Now, the same formula using SHOWIF:

=SHOWIF(SUMIFS(Payments!$D$2:$D$1000,Payments!$C$2:$C$1000,"="&B$1,
OFFSET(Payments!$D$2:$D$1000,0,MATCH($A3,Payments!$E$1:$Z$1,0)),"x") -
 SUMIFS(Payments!$D$2:$D$1000,Payments!$C$2:$C$1000,"="&$A3,
OFFSET(Payments!$D$2:$D$1000,0,MATCH(B$1,Payments!$E$1:$Z$1,0)),"x"), "<=0")

That’s MUCH shorter, and probably much easier to understand.

Here are the two functions:

' This function will display the value if it matches the condition;
' otherwise, it will display an empty cell, or
' the third (optional) parameter
Function SHOWIF(value As Variant, condition As String, _
 Optional nonvalue As String = "") As Variant
 If (Not IsError(value) And Application.Evaluate(CStr(value) & condition)) Then
 SHOWIF = value
 Else
 SHOWIF = nonvalue
 End If
End Function
' This function will display the value if it does not matches the condition;
' otherwise, it will display an empty cell, or' the third (optional) parameter
Function SHOWUNLESS(value As Variant, condition As String, _
Optional nonvalue As String = "") As Variant
 If (IsError(value) Or Application.Evaluate(CStr(value) & condition)) Then
 SHOWUNLESS = value
 Else
 SHOWUNLESS = nonvalue
 End If
End Function

To use them, just put in the formula =SHOWIF(value, condition) or =SHOWUNLESS(value, condition)

Grab the file SHOWFunctions.xla which is an Excel Add-in file containing both user-defined functions. It’s packaged as an Excel Add-in so you can easily add it to Excel without having to add the functions to each workbook.

To use it, save it as a file, then open Excel, and add it to the Add-in list. In Excel prior to 2007, you can do this using the Add-in command in the Tools menu (select ‘Browse’ from the dialog that opens and select the saved file to use it). In Excel 2007 and 2010, go to the Add-ins page in the Excel Options dialog, select ‘Excel Add-ins’ in the Manage box and press the Go button. Then use the browse button the same as in the previous versions.
Summary

These functions should make your spreadsheet code much easier to follow and therefore easier to debug. We hope you get a lot of use out of them! Perhaps you could improve or extend our ideas? Please let us know…

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 XLS conversion software.

For more Excel tips from Yoav, join him on Facebook or Twitter