Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share generic LAMBDA-functions over different projects?

Given a generic LAMBDA-UNION-function like this: https://stackoverflow.com/a/69861437/16578424:

What would be the best method to reuse it in several projects?

like image 201
Ike Avatar asked Oct 24 '25 07:10

Ike


1 Answers

I read somewhere that the developers are working on a way to share lambdas, but I haven't seen anything yet. In the meantime, sharing lambdas from workbook A to workbook B is as simple as copying any sheet (even blank) from workbook A to B (and then deleting it if you wish).

There is one nasty drawback - if the lambda already exists in B, Excel adds A's version with the same name, but limits scope to the copied sheet. This means it will apply only to the copied sheet, and other sheets will keep using the original lambda. (And if you then delete the copied sheet, the scope-limited lambda will disappear with it).

This means that if you want to enhance or correct an existing lambda, copying a sheet across is not going to do it. So I have written code to do it, below.

Sub CopyLambdas()
    Dim wb As Workbook, n, List
    'make a concatenated list of lambdas in this workbook
    List = "|"                                   'delimiter is |
    For Each n In ThisWorkbook.Names
        If InStr(1, n.value, "lambda", vbTextCompare) > 0 Then
            List = List & n.Name & "|"
        End If
    Next n
       
    'process all open workbooks (except this one of course)
    For Each wb In Workbooks
        If Not wb Is ThisWorkbook Then
            With wb
                For Each n In .Names             'look for lambdas
                    If InStr(1, n.value, "lambda", vbTextCompare) > 0 Then
                        'if this lambda has a name that's in our list, delete it
                        If InStr(1, "|" & n.Name & "|", n.Name, vbTextCompare) > 0 Then n.Delete
                    End If
                Next n
                ThisWorkbook.Sheets("Lambdas").Copy After:=.Sheets(.Sheets.Count)
            End With
        End If
    Next wb
End Sub
like image 170
dbb Avatar answered Oct 27 '25 15:10

dbb