Converting a formula to values

Excel has a built-in function (FormulaText) to display a formula as cell references, rather than the result value.  It would be useful to be able to display the value of each cell reference as well, but there is no built-in function to do that.

It turns out it’s not easy to do with VBA either.  I have added a macro to do the job to the Eval2 spreadsheet:

Sub Addr2Val()
Dim PrecCells As Variant, CellRng As Range, NumP As Long, i As Long, Form As String
Dim CellAddr() As String, CellVal() As Double, xCell As Range, OutCols As Long

    Set CellRng = Application.ActiveCell
    Form = CellRng.Formula
    Form = Replace(Form, "$", "")
    Set PrecCells = CellRng.Precedents
    NumP = PrecCells.Count
    ReDim CellAddr(1 To NumP, 1 To 1)
    ReDim CellVal(1 To NumP, 1 To 1)
    i = 1
    For Each xCell In PrecCells
        CellAddr(i, 1) = xCell.Address
        CellAddr(i, 1) = Replace(CellAddr(i, 1), "$", "")
        CellVal(i, 1) = xCell.Value
        i = i + 1
    Next
    Form = Eval(Form, CellAddr, CellVal, 0)
    OutCols = Selection.Columns.Count
    If OutCols > 1 Then OutCols = OutCols - 1
    CellRng.Offset(0, OutCols).Value = " " & Form
    Set CellRng = Nothing
    Set PrecCells = Nothing
End Sub

The macro uses the Range.Precedents method to return the address and value of each cell reference in the formula, then calls the Eval user defined function (UDF) to convert the cell addresses in the formula to their numerical values.  It would be convenient to put this in a function, which could be called from the spreadsheet, but when entered in a UDF the .Precedents method does not return information on the cell precedents, it returns the information for the cell itself.  If anyone has any suggestions for converting the macro to a working UDF, please leave a comment.

The macro has been set up to return the results in the cell to the right of the selected cell, or if three or more columns are selected, in the top right cell of the selected range.  Note that the macro will write over  any contents in the results cell.

An example of the macro in use (with instructions) is shown in the screen-shot below:

The updated spreadsheet, including full open-source code and the example above, can be downloaded from:

Eval2.ZIP

Posted in Excel, UDFs, VBA | Tagged , , , | 2 Comments

Excel to Alglib via xlwings

The Alglib library provides many valuable numerical analysis routines, but the VBA version is no longer actively maintained.  The latest versions are available in C++, C#, and Python, of which the Python version (in conjunction with xlwings) provides the easiest interface with Excel.

An Alglib/Python based spreadsheet has previously been presented here.  I have now updated this to the latest versions of xlwings and Alglib, added additional solver and linear algebra functions, and updated the Python code to make better use of the xlwings auto-import functionality, which generates the necessary VBA code at the touch of a button.  The new spreadsheet can be downloaded from:

xlAlglib.zip

As usual, the download files include full open source code.

To install the necessary files:

  1. Install Python, either version 2 or 3.  The Anaconda Python package will also install xlwings, but see below.
  2. Install or update to the latest version of xlwings (0.11.04 at the time of writing).  If the automatic install delivers an earlier version, see here.
  3. Install Alglib.
  4. Copy the contents of the xlAlglib zip file to any convenient location.
  5. Any problems, please let me know.

The screenshots below show the functions available in the new version.  Future posts will look in more detail at specific examples, and procedures for modifying the Python code.

List of functions:

Spline Functions:

Parametric Splines:

Rational Interpolation Function:

2D and 3D Interpolation:

Fit a spline to scattered data:

Polynomial fitting:

Non-Linear Fitting:

Fit Polynomial to scattered data:

Radial Basis Function Interpolation:

Levenberg-Marquardt optimisation:

Integration of Cubic Splines:

Differentiation of Cubic Splines:

Matrix algebra:

Posted in AlgLib, Curve fitting, Excel, Link to Python, Maths, Newton, Numerical integration, UDFs, VBA, xlwings | Tagged , , , , , , , , , | 12 Comments

Installing xlwings 0.11.4

The 0.11 versions of xlwings have some major changes to the arrangement of the required files, which will make installation and updating much easier, but for some reason Anaconda is very slow to upload the new versions, and the pdf documentation has some omissions, so here is a summary of procedures for downloading and installing the latest version.

Finding the latest version

The xlwings web site shows the latest version.  The source code may be downloaded from the GitHub page, but for easier installation use pip or conda:

pip install xlwings or conda install xlwings

The main conda install is still on version 0.10 however, so for those using the Anaconda Python package, and wanting to stick with conda for updates use:

conda install c condaforge xlwings

Install the add-in

  1. If you have the old xlwings addin installed, find the location and remove it or overwrite it with the new version (see next step). If you installed it via the xlwings command line client, you should be able to do: xlwings addin remove.
  2. Close Excel. Run xlwings addin install from a command prompt. Reopen Excel and check if the xlwings Ribbon appears. If not, copy xlwings.xlam (from your xlwings installation folder under addin\xlwings.xlam manually into the XLSTART folder. You can find the location of this folder under Options > Trust Center > Trust Center Settings… > Trusted Locations, under the description Excel default location: User StartUp. Restart Excel and you should see the add-in.

Upgrade existing workbooks

  1. Make a backup of your Excel file
  2. Open the file and go to the VBA Editor (Alt-F11)
  3. Remove the xlwings VBA module
  4. Add a reference to the xlwings addin, see Installation
  5. If you want to use workbook specific settings, add a sheet xlwings.conf, see Workbook Settings

For more details see the xlwings documentation.  For the current version (0.11.4) the pdf documentation has the “Migrate to Version 0.11” and “installation” sections missing.  This will be fixed in future versions, but for now use the on-line docs.

Posted in Excel, Link to Python, VBA, xlwings | Tagged , , | 1 Comment

Using the Frame Analysis Spreadsheets

Following some recent questions in the comments, here are some answers:

How can nodal moments be created?

The current versions only allow for forces to be applied to beams, but any point load or moment applied at the start or end of a beam will in effect be applied at the node, so set “Position” to zero for any beam starting at the node, or to the beam length for a beam ending at the node.  In Frame 4 (the 2D version) all moments are applied about the Z axis.  In 3DFrame moments are applied about the global axis specified in “Direction XYZ” (Column C).

How can hinge or spherical joints, or bushing joints be created?

Both Frame4 and 3DFrame allow either end of a beam to be given a rotational spring stiffness.  If the stiffness is set to a very low value the beam ends will in effect be free to rotate.  In Frame4 rotations are about the Z axis.  In 3DFrame the rotations are relative to the beam principal axes, which are described here:  3D Frames, axes and stiffness matrices.

3DFrame also allows beams ends to be allocated a translational spring release along any of the three principal axes.

The example frame in each of the download files illustrates the use of the beam end springs.

How can multiple point restraints be applied?

Nodes can in effect be restrained to move together by connecting with very stiff members, with any required end hinge conditions, to provide the required restraint.

 

Posted in Excel, Finite Element Analysis, Frame Analysis, Newton, VBA | Tagged , , , , | 2 Comments

Installing Frame4 and 3DFrame

Following a recent comment I have added a VBA only version to the download files of the frame analysis spreadsheets:

3DFrame.zip

Frame4.zip

The VBA versions should work on any version of Excel from 2007, with macros enabled, without any further installation.

The standard versions link to compiled versions of the solver code, that provide much better performance with large frames.  The download file includes the required dll files, and the installation process is summarised here:

Installing C# dll files, reminder

Any problems, please leave a message here.

 

Posted in AlgLib, Excel, Frame Analysis, Link to dll, Newton, VBA | Tagged , , , , , , | 1 Comment