Forum Discussion
Advanced Formula Editor: converting functions Names into a module
I've written a group of complicated functions using the AFE in Excel Labs. They are stored in the Name Manager. I now realize it would have been better to write these using the Modules editor. Is there a way to export Lambdas from the list of Functions in the Names tab of the AFE, into a Module (not a vba module, a Module within AFE).
4 Replies
- DavidJMorrisCopper Contributor
Hi folks, actually, this is easy to do, I wrote some VBA code that does this, since the NameManager is an object exposed in Excel VBA--not terribly elegant, little error correction, and it doesn't work with files stored on OneDrive/SharePoint because the file names are a headache. Save your file on a local drive, as an XLSM macro enabled file, copy this code into a module in VBA, then copy and paste the text file content into an Excel Lab Module:
Option Explicit Sub ExportLambdasToTextFile() 'Code to export Excel Lab Functions written in Names editor, to text that can be copied into a module 'David Morris, 2025, free to use Dim nm As Name Dim fso As Object ' FileSystemObject Dim ts As Object ' TextStream Dim filePath As String Const LambdaTag As String = "=LAMBDA" 'tag to filter for lambdas only Dim LambdaLen As Integer Dim CleanRef As String ' Define the path and filename for the output text file filePath = ThisWorkbook.Path & "\NamedLambdas.txt" ' Saves in the same directory as the workbook; doesn't work for OneDrive files 'filePath = "D:\NamedRanges.txt" ' Create a FileSystemObject Set fso = CreateObject("Scripting.FileSystemObject") ' Open the text file for writing. True creates a new file or overwrites an existing one. Set ts = fso.CreateTextFile(filePath, True) LambdaLen = Len(LambdaTag) ' Write a header to the text file ts.Writeline "Excel Named Lamdas for Excel Labs Workspace" ts.Writeline "------------------" ' Loop through each named range in the workbook For Each nm In ThisWorkbook.Names If Left(nm.RefersTo, LambdaLen) = LambdaTag Then ' Write the name and its reference to the text file, in tabbed format for copy and paste into Excel, to import from grid into workspace 'ts.WriteLine nm.Name & vbTab & nm.RefersTo & vbTab & "/**" & nm.Comment & "*/" 'Better solution: write text formatted to copy and paste into Module 'Output the comment formatted to be grabbed into the name space ts.Writeline "/**" & nm.Comment & "*/" 'output the Lambda def ts.Writeline nm.Name & " " & nm.RefersTo & ";" 'output blank line ts.Writeline End If Next nm ' Close the text file ts.Close ' Clean up objects Set ts = Nothing Set fso = Nothing MsgBox "Named Lamdas exported successfully to, ready to copy and paste into Excel labs module: " & filePath, vbInformation End Sub - DavidJMorrisCopper Contributor
Thanks to both of you. I will test out Patrick's approach and see if it works. I am looking also to be able to add comments...
- SergeiBaklanDiamond Contributor
Here Import module from Name Manager · Issue #56 · microsoft/advanced-formula-environment author of AFE suggests some workaround. I didn't test it and that's 2 years old, but who knows...
- Patrick2788Silver Contributor
I don't believe there's an elegant solution at the moment. You could scoop each function out of the name manager, place them in a cell (it will evaluate to a #CALC! error), and then import from grid from the Modules tab of AFE.
I used to build functions through Names | Functions but the space is very limited. Everything begins and ends in the workbook module!