Links to link VBA to C#

If you search the Internet for information on how to link Excel VBA to C# you will find thousands, if not millions, of links with details of how to connect to Excel from C#, but very few about going the other way, which is what I want to do.  The link below gives detailed, step by step, instructions for doing that, allowing functions in a C# library to be called from a VBA routine with the minimum of hassle.  The link gives a very simple “hello world” example, and I will be giving some more useful examples, linking to the ALGLIB library, in future posts.

A Beginner’s Guide to calling a .NET Library from Excel

Introduction

It’s actually very easy to call a .NET library directly from Excel, particularly if you are using Visual Studio 2005. You don’t need Visual Studio Tools for Office. However there doesn’t seem to be an easy guide on the internet anywhere. MSDN help is quite good on the subject, but can be a little confusing. This article is an attempt to redress the situation.

This article was updated 24th August 2007 to cover Excel 2007 and to clarify the issues with intellisense.

…    More at link

Reading the comments at the link, it seems there is a problem linking with the 64 bit version of Office 2010, so those wanting to do that have a look at the latest comments first.  There is no problem with 64 bit Windows 7, which is what I am using (with 32 bit Office 2010).

The second link is to the MSDN Library, giving details of how to link different data types to objects in C#.  I needed this for one of the ALGLIB routines I will be looking at.  It’s probably very basic stuff, but for those not familiar with C#, it’s not that easy to find.

Boxing and Unboxing (C# Programming Guide)

Boxing and unboxing enable value types to be treated as objects. Boxing a value type packages it inside an instance of the Object reference type. This allows the value type to be stored on the garbage collected heap. Unboxing extracts the value type from the object. In this example, the integer variable i is boxed and assigned to object o.

… see link for example code.

Posted in Excel, Link to dll, VBA | Tagged , , , , , | 11 Comments

More moves through the fair

Following the last Bach instalment, here are three more versions of She Moves Through the Fair.

The first is the “official” Fairport Convention release, with Sandy Denny on vocals.  Much more polished than the demo version posted last time, but it doesn’t capture the same ghostly feel, to my mind.

The second is a live performance from Richard Thompson in 1990 (who would also have been playing lead guitar on the Fairport version), and finally an instrumental version from a very Young Jimmy Page (probably recorded around 1969), with a dash of Blackwater Side, thrown in for good measure.

If you catch a glimpse of the ghosts of Davy Graham or Bert Jansch flitting through the background in the last one, it wouldn’t be at all surprising.

Posted in Bach | Tagged , , , , , , | 3 Comments

Polynomial Update

Following a comment here: Solving cubic and quartic equations with Excel the Quartic and CubicC User Defined Functions (UDFs) have been amended to fix a problem occurring with some combinations of coefficients.

The updated spreadsheet (including full open source code) can be downloaded from: Polynomial.zip

As well as functions to solve Quartic, Cubic and Quadratic equations analytically the spreadsheet includes an iterative method that will solve polynomial functions of any order, using the Jenkins-Traub method, and a function to evaluate polynomial functions of any order.

Posted in Excel, Maths, Newton, UDFs, VBA | Tagged , , , , , , , | 12 Comments

Mohr’s Circle and Plate Stresses and Actions

The functions described in this post can be found in the download file PlateStress.zip, including examples and full open source code.

PlateStress Functions, click for full size view

The function MCircle returns the plane stress components (XX, YY and XY stress) for any specified stress field, when rotated through a specified angle.  If no angle is specified the function returns the principal stresses, and the principal stress angle to the X axis of the original stresses.

MCircle is used in the function PlateStress4, which returns the in-plane forces and moments on a finite element analysis plate element, given the stresses at 4 Gauss Points.  The stresses may either be specified in the element local axis system, or the global XY system, in which case the angle between the X axis and the element longitudinal axis at each section must be specified.  Examples are included in the spreadsheet and illustrated below.

The first example looks at an inclined simply supported beam, modelled with 8 noded plate elements in the finite element package Strand7:

Strand7 results for inclined beam

The spreadsheet analysis of the stresses extracted from Strand7 is shown below, including:

  • Global stresses from the FE analysis
  • Stresses converted to the element local axes with the MCircle function
  • Beam actions found using the global stresses with a rotation angle
  • Beam actions from the local stresses with no rotation angle (identical results).

Plate stresses and beam actions in an inclined beam

The second example is a circular arc subject to vertical point loads, and modelled using 8-noded plate elements and with short straight beam elements:

Strand7 analysis of a circular arc modelled with plate elements and beam elements

The analysis results are shown below, with Strand7 global plate stresses, plate actions found with the PlateStress function, and for comparison beam actions from the analysis using short beam elements:

PlateStress results from Strand7 analysis of a circular arc

Plate results compared with Strand7 beam results for bending and shear force

Posted in Arrays, Beam Bending, Excel, Finite Element Analysis, Frame Analysis, Newton, Strand7, UDFs, VBA | Tagged , , , , , , | 1 Comment

Ghost Stories

The first video was directed by my daughter whilst at the Australian Film Television and Radio School last year, and weaves together stories of hard times in the early days of Sydney’s Newtown district:

Plug in the headphones, switch off the lights, start the video, and click on full-screen

The second combines a poor quality demo recording of Sandy Denny singing “She Moves Through the Fair” with blurred and grainy video of “The Fall of the House of Usher”.  The result is simply perfect:

Posted in Bach, Excel, Films, Newton | Tagged , , , | 1 Comment