One-button import of VBA module to Excel

Mark Travis 30 Reputation points
2023-05-30T01:58:46.9766667+00:00

I have a website that helps a user create a specialized mathematical formula. The website then builds an Excel Custom Function in Visual Basic. I've had my users cut and paste the VB code from a dialog on my website to the Developer Section of Excel by "Insert Module.." followed by pasting the VB code and saving it.

My users are about as error prone as you can get. They cut and paste incompletely, or they will have several Excel sheets open and import the VB in to the wrong worksheet. I need to come up with something stupid simple.

Ideally, I'd like to have one button in the ribbon bar that represents a way to import a "customfunction.bas" into an Excel workbook without them having to get involved with the "Developer" area at all. (They are data scientists, not programmers.)

I've found VB code that triggers a dialog box to open a file, but I can't seem to string together the rest of the logic to make this happen. Any hints??

Sub ImportVBAToCurrentSheet()

Dim FileName As String

FileName = Application.GetOpenFilename()

MsgBox FileName

End Sub

Microsoft 365 and Office Development Other
Microsoft 365 and Office Excel For business Windows
{count} votes

Accepted answer
  1. Tanay Prasad 2,250 Reputation points
    2023-05-30T06:28:16.7433333+00:00

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.