+ Reply to Thread
Results 1 to 11 of 11

Changing workbook code via vba

Hybrid View

Journeyman3000 Changing workbook code via vba 10-12-2014, 05:40 PM
LJMetzger Re: Changing workbook code... 10-12-2014, 07:40 PM
Norie Re: Changing workbook code... 10-12-2014, 08:15 PM
Journeyman3000 Re: Changing workbook code... 10-12-2014, 09:23 PM
Norie Re: Changing workbook code... 10-12-2014, 11:18 PM
Journeyman3000 Re: Changing workbook code... 10-12-2014, 11:25 PM
xladept Re: Changing workbook code... 10-13-2014, 12:25 AM
HaHoBe Re: Changing workbook code... 10-13-2014, 12:36 AM
mikerickson Re: Changing workbook code... 10-13-2014, 12:45 AM
Norie Re: Changing workbook code... 10-13-2014, 04:47 AM
Journeyman3000 Re: Changing workbook code... 10-13-2014, 05:55 PM
  1. #1
    Forum Contributor
    Join Date
    03-05-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    301

    Changing workbook code via vba

    Hello all,

    I have code for my sheets (e.g Sheet1(Main)) that runs all the activation and change events etc. I also have various modules.

    I have code that will modify the modules: for example, this follow code deletes a module:

    Set vbCom = Application.VBE.ActiveVBProject.VBComponents
    vbCom.Remove VBComponent:=vbCom.Item("Mod_MyModule")
    However, I don't know how to manipulate subroutines within the sheet module.

    In principle, I would like to locate a string within a Sub, and change it. Or, if I have to change the entire sheet module without affecting the sheet itself.

    How can this be done - can anyone point me in the right direction. I look at CPearson stuff but it didnt seem to provide this particular answer.

    Cheers

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Changing workbook code via vba

    Hi Journeyman3000,

    See the attached file and/or code in post #3 of the following thread. If it doesn't solve your problem, the references may point you in the right direction. http://www.excelforum.com/excel-prog...de-in-vba.html

    Lewis

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Changing workbook code via vba

    Why do you want to do this?
    If posting code please use code tags, see here.

  4. #4
    Forum Contributor
    Join Date
    03-05-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    301

    Re: Changing workbook code via vba

    Thank you for your responses. I was kinda hoping for a shorter solution in which I could provide the sheetname, locate the string in the VBE editor and change it. I'll search through the solution and see if I can find it there. cheers

    Norrie: I want to do it becuase I've distributed the workbook to mulitple people, and I need to update the code in each. I can do this by looping through each book, looking for the correct worksheet, updating the VBE ect. In this case, it's just a single string. Cheers

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Changing workbook code via vba

    Why not use an add-in?

  6. #6
    Forum Contributor
    Join Date
    03-05-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    301

    Re: Changing workbook code via vba

    I don't know what add-in to use, and I beleive it should be acheviable with code.

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Changing workbook code via vba

    Hi Journeyman,

    Try using the forum search with "VBComponent" - I've found several entries that seem pertinent to your task
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  8. #8
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Changing workbook code via vba

    Hi, Journeyman3000,

    I´d have a read at parameters and pass the information you want to change via parameter, a list in a worksheet, a name in the name manager, a text file rather than changing code.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  9. #9
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Changing workbook code via vba

    The best way to update the modules in others workbooks is to create exported files of the modules and have them run a routine that deletes the existing module and imports the files (exports) that you provide.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Changing workbook code via vba

    You would create the add-in, distribute it, edit it, redistribute it etc.

    Using code to create/change code can be done but can cause problems.

    For example security settings would need to be changed (lowered).

    PS If it's just a single string why not store it somewhere instead of hard-coding it?

  11. #11
    Forum Contributor
    Join Date
    03-05-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    301

    Re: Changing workbook code via vba

    Thank you all for for advice.

    Using addins is a good idea, but for this particular excercise I needed to change the code in the VBE for Sheet1. It turns out there are several locations that needed updates so I can pretty much use the same code, although lesson learned for the future. I found the following code, and modified it slightly and it works as required.

    Function CodeRewrite(SheetName As Workbook)
    Dim VBP As VBIDE.VBProject
    Dim VBC As VBIDE.VBComponent
    Dim SL As Long, EL As Long, SC As Long, EC As Long
    Dim S As String
    Dim Found As Boolean
     
        On Error Resume Next
        Set VBP = SheetName.VBProject
        On Error GoTo 0
         
        If VBP Is Nothing Then
            MsgBox "Your security settings do not allow this macro to run.", vbInformation
            Exit Function
        End If
         
        For Each VBC In VBP.VBComponents
            If VBC.Type = vbext_ct_Document Then
                If InStr(1, VBC.Name, "ThisWorkbook", vbTextCompare) = 0 Then
                    If VBC.Name = "Sheet1" Then
                        With VBC.CodeModule
                            SL = 1 ' Start Line
                            SC = 1 ' Start Column
                            EL = .CountOfLines ' End Line
                            EC = 999 ' End Column
                            Found = .Find("Find This String", SL, SC, EL, EC, True, False, False)
                            If Found = True Then
                                S = .Lines(SL, 1)
                                S = Replace(S, _
                                    "Find This String", _
                                    "Replace With This String", 1, -1, vbTextCompare)
                                Debug.Print S
                                .ReplaceLine SL, S
                            End If
                        End With
                        Exit Function
                    End If
                End If
            End If
        Next VBC
         
    
    End Function
    Thanks to all who provided advice.

    Cheers

+ 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] Auto-Closing of Workbook Keeping me from Changing VBA Code?
    By dac5039 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-17-2014, 10:24 PM
  2. [SOLVED] Need code for changing workbook on time ?
    By black7 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-20-2013, 05:08 PM
  3. changing code from New worksheet to new workbook then new sheets.
    By clemsoncooz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-16-2012, 12:52 PM
  4. Changing Code To Modify Entire Workbook
    By sktyrhrtout in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-08-2008, 07:28 PM
  5. Changing Workbook Code Using a Macro
    By jamesryan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-07-2008, 04:05 AM

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