+ Reply to Thread
Results 1 to 4 of 4

Updating VBA code without replacing the entire workbook.

Hybrid View

auglocqnuk Updating VBA code without... 11-26-2022, 07:44 PM
Corbett Re: Updating VBA code without... 11-27-2022, 04:06 AM
ByteMarks Re: Updating VBA code without... 11-28-2022, 01:12 PM
Greg M Re: Updating VBA code without... 12-03-2022, 08:39 PM
  1. #1
    Registered User
    Join Date
    10-02-2022
    Location
    Tucson
    MS-Off Ver
    Varies
    Posts
    13

    Updating VBA code without replacing the entire workbook.

    I have a few workbooks containing thousands of lines of code that I have distributed to my organization. All of the workbooks contain user forms through which various people within my organization are able to manipulate data (this includes loading data from outside the immediate workbook). However, whenever I make changes/updates to the VBA code, I either have to

    1) export the data from the existing file, copy the new file to the directory, then import it into the new file with the updated code, or
    2) manually copy the code into the existing file

    Is there an easy way to update VBA code in a workbook without changing anything else whatsoever, as if I were simply updating a piece of software?

    Thanks

  2. #2
    Registered User
    Join Date
    04-19-2021
    Location
    Dublin, Ireland
    MS-Off Ver
    O365, Win10 and Mac
    Posts
    47

    Re: Updating VBA code without replacing the entire workbook.

    Create an addin (xlam) instead. That will contain only the code. The data can then be stored in a normal xlsx file. There's lots of info about creating addins on the web, for example https://trumpexcel.com/excel-add-in/

  3. #3
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,966

    Re: Updating VBA code without replacing the entire workbook.

    If it's a one off you could use something like to to import the updated modules

    https://www.rondebruin.nl/win/s9/win002.htm

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Updating VBA code without replacing the entire workbook.

    Hi there,

    See if the following approach gets you moving in the right direction - it requires a Reference to be set to "Microsoft Visual Basic for Applications Extensibility 5.3".

    Use the following code in your master workbook to export all of its VBA code to a shared directory (e.g. H:\VBA Modules):

    
    Sub ExportModules()
    
        Const sFOLDER_PATH  As String = "H:\VBA Modules"
    
        Dim sExtension      As String
        Dim sFileName       As String
        Dim sFullName       As String
        Dim cmp             As VBComponent
        Dim vbp             As VBProject
    
        Set vbp = ThisWorkbook.VBProject
    
        For Each cmp In vbp.VBComponents
    
            If cmp.Type <> vbext_ct_Document Then
    
                sExtension = vbNullString
    
                Select Case cmp.Type
    
                       Case vbext_ct_ClassModule
    
                            sExtension = ".cls"
    
                       Case vbext_ct_MSForm
    
                            sExtension = ".frm"
    
                       Case vbext_ct_StdModule
    
                            sExtension = ".bas"
    
                End Select
    
                If sExtension <> vbNullString Then
                    sFullName = sFOLDER_PATH & "\" & cmp.Name & sExtension
                End If
    
            End If
    
        Next cmp
    
    End Sub


    Create a VBA CodeModule named M99_ImportVBA in each of the User workbooks whose VBA you wish to update regularly, and insert the following code into it:

    
    
    
    Option Private Module
    Option Explicit
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Public Sub UpdateVbaComponents()
    
        Call DeleteModules
    
        Call ImportModules
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub DeleteModules()
    
        Const sIMPORT_VBA   As String = "M99_ImportVBA"
    
        Dim cmp             As VBComponent
        Dim vbp             As VBProject
    
        Set vbp = ThisWorkbook.VBProject
    
        For Each cmp In vbp.VBComponents
    
            If cmp.Name <> sIMPORT_VBA Then
    
                If cmp.Type <> vbext_ct_Document Then
                    vbp.VBComponents.Remove VBComponent:=cmp
                End If
    
            End If
    
        Next cmp
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub ImportModules()
    
        Const sFOLDER_PATH  As String = "H:\VBA Modules"
    
        Dim sExtension      As String
        Dim objFolder       As Object
        Dim objFile         As Object
        Dim objFSO          As Object
        Dim cmp             As VBComponents
    
        Set cmp = ThisWorkbook.VBProject.VBComponents
    
        Set objFSO = CreateObject(Class:="Scripting.FileSystemObject")
    
        Set objFolder = objFSO.GetFolder(sFOLDER_PATH)
    
        If objFolder.Files.Count > 0 Then
    
            For Each objFile In objFolder.Files
    
                sExtension = objFSO.GetExtensionName(objFile.Name)
    
                If sExtension = "cls" Or _
                   sExtension = "frm" Or _
                   sExtension = "bas" Then
    
                    cmp.Import Filename:=objFile.Path
    
                End If
    
            Next objFile
    
        End If
    
    End Sub


    Finally, insert the following code into the "ThisWorkbook" VBA CodeModule:

    
    
    
    Option Explicit
    
    
    Private Sub Workbook_Open()
    
        Call UpdateVbaComponents
    
    End Sub

    Using the above approach means that your updated VBA CodeModules need to be exported only to the shared folder. Each time a User workbook is opened, all of its VBA CodeModules will be deleted, and those from the shared folder will be imported to replace them.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M
    Last edited by Greg M; 12-03-2022 at 08:47 PM. Reason: Original version was posted accidentally

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Apply code to entire workbook.
    By kgall89 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-26-2018, 08:49 AM
  2. Replacing Hyperlinks in an Entire Workbook
    By js0873 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-27-2017, 01:32 PM
  3. Updating Current Workbook with New Version Via Code?
    By CraigsWorld in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-21-2016, 02:53 AM
  4. New to VBA - Problem applying code to the entire workbook
    By itobon in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-28-2016, 01:46 PM
  5. VBA code for Count if funtion for entire workbook
    By VINOTHBASKRAN in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-13-2014, 05:07 AM
  6. Code runs on entire workbook...need it to only run on one worksheet
    By jrobertson2403 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-28-2013, 04:50 PM
  7. adding data to another workbook (replacing entire record if value in cell A matches)
    By ecc34_11 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-29-2011, 08:47 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1