Hi,
You can use the following approach:
- Create a new Excel workbook (let's call it "Import Custom Function.xlsm") and add a custom ribbon tab with a single button. This button will trigger the import process.
- In the Visual Basic Editor, create a new module in the "Import Custom Function.xlsm" workbook.
- In the module, write the code that will handle the import process. Here's an example code snippet to get you started:
Sub ImportVBAToCurrentSheet()
Dim FileName As String
Dim VBModule As Object
FileName = Application.GetOpenFilename("VB Files (*.bas), *.bas")
If FileName <> "False" Then
Set VBModule = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_StdModule)
VBModule.CodeModule.AddFromFile FileName
MsgBox "VBA module imported successfully!", vbInformation
End If
End Sub
- Save the "Import Custom Function.xlsm" workbook with the custom ribbon tab and module.
- Distribute the "Import Custom Function.xlsm" file to your users.
When users open the "Import Custom Function.xlsm" workbook, they will see the custom ribbon tab with the button. Clicking on the button will trigger the ImportVBAToCurrentSheet
macro. It will prompt the user to select a .bas file containing the VBA code. Once a valid file is selected, the code will create a new module in the current workbook's VBA project and import the code from the selected file. A message box will confirm the successful import.
I hope this works!
Best Regards.