3

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?

ZygD
  • 22,092
  • 39
  • 79
  • 102
Ike
  • 9,580
  • 4
  • 13
  • 29
  • 1
    Take a look at : [this video](https://www.youtube.com/watch?v=IPP4MVb25mU) – Ron Rosenfeld Nov 07 '21 at 13:17
  • @RonRosenfeld this one is really helpful - it's so trivial that i wouldn't have thought of it. I will not use a blank sheet but one that contais documentation about the LAMBDA-functions - including the parameters used - and then copy this sheet to other projects. – Ike Nov 07 '21 at 17:02

1 Answers1

2

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
T.M.
  • 9,436
  • 3
  • 33
  • 57
dbb
  • 2,827
  • 18
  • 16