UDF

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!

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

Excel DNA Hello World Function

OK, enough with the back story, lets get down to business.

In this post we are going to look at the simplest way to use Excel DNA. This methods is ideal if you want to write User Defined Functions (UDF), or want to copy a UDF from VBA to DNA for a quick and easy* performance improvement.

In this example we are going to use the XML file to hold our code, so there is no need to have an IDE installed. In following instalments we will look at how to use a IDE to write a compliled DLL and use that with Excel DNA.

First things first, we need to get hold of the Excel DNA files. Download the most up to date version of Excel DNA from the Excel DNA codeplex site here:

http://exceldna.codeplex.com/

Once you have down loaded the files, extract them to a new folder, called something like “XL DNA Tests”, there should be 2 folders in there, like this:

Open the distribution folder, find and copy the XLL file called “ExcelDNA.xll”, now paste that back in to your root folder, it should look like this:

Next create a new text file (use the short cut menu, right click>>New>>Text Document). Rename that text file to “MyFirstDNAAddin.DNA”, then remane the XLL file to the same name, but keep the .xll extention, i.e “MyFirstDNAAddin.xll”, it should all look like this:

Note that the 2 files have the same name. This is very important as it ties the code we are going to write in the DNA file with the XLL. When we load the XLL, it will be looking for code in a file with the same name as itself . Notice that for this to work the XLL and the DNA file must be on the same path – i.e. in the same folder, at the same level, like they are in this example.

Now time for the code. Open the DNA file (double click the icon it will open with notepad) and copy and paste the following code into the DNA file.

<DnaLibrary>
  <![CDATA[
     Public Module Mod1
        Function MyFirstFunction(byVal YourText As string)as string
          MyFirstFunction = YourText & ",...Hello World!"
        End Function
      End Module
   ]]>
</DnaLibrary>

Close the file, saving the changes and then double click the XLL file. This will open Excel, enable macros if required. With a new workbook open, open the function wizard, and find the category called “MyFirstDNAAddin”…

Select that category, and you will see your new function in the functions list, like this:

Click OK, and enter some text in the argument box, then hit OK, your function will appear in the active cell, something like this:

And that’s really all there is to it. To share this new function all you need to do is send out the XLL file and the DNA file to the end user, get them to save the file to their PC, then get them to add the addin to Excel via the addins manager in the Tools menu, henceforth the function will always be available to that user on that PC.

OK lets take a step back and look at what we just did.

The first 2 lines of code in the text file are:

<DnaLibrary>
  <![CDATA[

These are just XML element tags, they are used by Excel DNA to help it read in your code, they are not part of the function.

The next line opens a new Module and gives it a name “Mod1”, later this module is closed with “End Module”

Function MyFirstFunction(byVal YourText As string)as string
      'Other code here
End Module

The actual code that does the work are these three lines…

Function MyFirstFunction(byVal YourText As string)as string
      MyFirstFunction = YourText & ",...Hello World!"
End Function

“Function” Declares a new function, which we call “MyFirstFunction”, a bracket, then an argument is defined, another bracket, then the return type of the function. The next line returns the result to the function, which is the text you passed in the Argument “YourText” and the string, “,…Hello World!”. Finally "End Function" closes the function.

Then

]]>
</DnaLibrary>

Are just the closing parts for the XML trees.

Very simple stuff if you’ve written VBA I’m sure you’ll agree. In the next post will look at taking a VBA function and porting it over to Excel DNA, in real live so to speak…

*Generally VBA code transfers quite well in to VB.Net code, some times you need to change the syntax a  little, some times you need to use a dot net library etc., start with something small and simple and work up, you’ll soon get the hang of dot Net.

The MIE Podcasts – Govert van Drimmelen

In this podcast we caught up with Excel DNA author Govert van Drimmelen. Govert is a Mathematics lecturer at the University of Johannesburg, in South Africa. Excel DNA has had some attention from the Excel blog sphere in the past few months, Simon, Mike, and Mathias have all posted about it. I’ve been using it for a while now, and it’s brilliant. Here we talked about how Excel DNA came about, the basics of how it works, some of the new features and what’s planned for the further.

Govert van Drimmelen, famous for his Tom Selleck impression

If you don’t know what Excel DNA is, well then listen to the podcast, but in short it’s a free and easy way to get .Net code into Excel.

I’m sure you will all join with me in thanking Govert, not only for making time for the podcast, but also for all the hard work he’s done  in getting Excel DNA out of the door – well done Govert!!!

And remember if you’d like to see more of Excel DNA, I’m giving a talk about it at the UK Excel Dev Con, in a weeks time!!!

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.