Results 1 to 4 of 4

Updating VBA code without replacing the entire workbook.

Threaded View

  1. #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

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