Results 1 to 14 of 14

Thread: Programmatically Update Module

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Programmatically Update Module

    Hi,

    I am looking for a way to update the same module in over 100 files.

    All i need to do is update a variable value from 24 to 8.

    I know you are able to update lines of code in modules, but I don't know how.

    Could someone please offer some sample code/examples?

    Thanks
    David

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Programmatically Update Module

    you can try like this i tested works ok, this method read the whole module, then deletes all code in the module then rewrites the module with the updated code
    vb Code:
    1. Dim vc As Object
    2. Set vc = Workbooks("book1").VBProject.VBComponents("module1").codemodule
    3. eend = vc.countoflines
    4. if vc.Find("mystr = 8", 1, 1, eend, 1, False, False, False) then  'only do anything if that variable and value are found in the module
    5. mystr = vc.Lines(1, eend)
    6. repstr = Replace(mystr, "mystr = 8", "mystr = 24")  ' mystr being the variable value to change
    7. vc.deletelines 1, eend
    8. vc.insertlines 1, repstr
    9. end if
    it is also possible to just replace individual lines, but it is more complicated code, and i doubt it has much gain in speed, unless the code in each module is very large
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: Programmatically Update Module

    Spot on mate, that works a treat

    bit gutted the intellisense (sp) doesn't work on the find/replace/deletelines and insertlines functions you posted

    will search for them now though

    just to confirm dim eend as long, mystr and repstr as string?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: Programmatically Update Module

    wont let me leave you rep, says i need to spread it about a bit before giving it to you again ha!

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: Programmatically Update Module

    Excel keeps encountering an error when i set it off on its own

    if i step through the code it works fine

    any ideas?

    vb Code:
    1. Sub open_workbooks()
    2. Dim cell As Range
    3.  
    4. For Each cell In Range("A2", Range("A2").End(xlDown))
    5.     Application.EnableEvents = False
    6.     Workbooks.Open (cell.Value)
    7.     Application.EnableEvents = True
    8.     update_Timer
    9.    
    10. Next cell
    11.  
    12. End Sub
    13.  
    14.  
    15. Sub update_Timer()
    16. Dim vc As Object
    17. Dim eend As Long
    18. Dim mystr As String, repstr As String
    19. Set vc = ActiveWorkbook.VBProject.VBComponents("module1").codemodule
    20. eend = vc.countoflines
    21. If vc.Find("TIMERSEC = 25", 1, 1, eend, 1, False, False, False) Then  'only do anything if that variable and value are found in the module
    22. mystr = vc.Lines(1, eend)
    23. repstr = Replace(mystr, "TIMERSEC = 25", "TIMERSEC = 8")  ' mystr being the variable value to change
    24. vc.deletelines 1, eend
    25. vc.insertlines 1, repstr
    26. End If
    27. ActiveWorkbook.Save
    28. ActiveWorkbook.Close
    29. End Sub

  6. #6
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Re: Programmatically Update Module

    Where does the error occur.

    Would I be correct in saying that you have the names of workbooks you want
    to operate on in column A of the Workbook the macro is run from?
    Signature Under Construction

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: Programmatically Update Module

    yeah all my file names are in column A

    I can't see where the error occurs, as when i step through the code it works fine

    it's when i set it off running on its own, its causes excel to crash

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Programmatically Update Module

    you should not save or close activeworkbook, specify the workbook by name or object, looks like you may try to close the workbook where your code is

    if you want intellisense to work try the code like this, pass the workbook or workbook name to your sub /function, i would use a function with a return of true or false is successful
    vb Code:
    1. dim w as workbook
    2. Set w = Workbooks("somefile.xls")
    3. With w.VBProject.VBComponents("module1").CodeModule
    4.     eend = .CountOfLines
    5.     mystr = .Lines(1, eend)
    6.     repstr = Replace(mystr, "mystr = 8", "mystr = 24")
    7.     .DeleteLines 1, eend
    8.     .InsertLines 1, repstr
    9. End With
    10. w.save
    11. w.close
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: Programmatically Update Module

    still not working

    modified code is below

    vb Code:
    1. Sub open_workbooks()
    2. Dim cell As Range
    3.  
    4. For Each cell In Range("A2", Range("A2").End(xlDown))
    5.     Application.EnableEvents = False
    6.     Workbooks.Open (cell.Value)
    7.     Application.EnableEvents = True
    8.     update_Timer
    9.     cell.Interior.Color = 13434828
    10. Next cell
    11.  
    12. End Sub
    13.  
    14. Sub update_Timer()
    15. Dim vc As Object
    16. Dim eend As Long
    17. Dim mystr As String, repstr As String
    18. Dim w As Workbook
    19.  
    20. Set w = ActiveWorkbook
    21.  
    22. If w.VBProject.VBComponents("XTAQ").codemodule.Find("TIMERSEC = 25", 1, 1, eend, 1, False, False, False) Then  'only do anything if that variable and value are found in the module
    23. With w.VBProject.VBComponents("XTAQ").codemodule
    24.     eend = .CountOfLines
    25.     mystr = .Lines(1, eend)
    26.     repstr = Replace(mystr, "mystr = 8", "mystr = 24")
    27.     .DeleteLines 1, eend
    28.     .InsertLines 1, repstr
    29. End With
    30. End If
    31. w.Save
    32. w.Close
    33. End Sub

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Programmatically Update Module

    i copied your code into excel module with a few minor changes, as none of my workbooks have a module of that name, put a couple of workbook names in column A, you code ran fine, both workbook modules had update code

    so what is not working?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Re: Programmatically Update Module

    I think most of the problem may be solved if you stop using ActiveWorkbook.

    Although you use w as the Workbook you are still closing it, and it is
    explicitly set to be the ActiveWorkbook by the following

    Code:
    Set w = ActiveWorkbook
    so you are really still trying to close the ActiveWorkbook.

    Try the following.

    vb Code:
    1. Sub open_workbooks()
    2. Dim cell As Range
    3.  
    4. For Each cell In Range("A2", Range("A2").End(xlDown))
    5.     Application.EnableEvents = False
    6.     Application.EnableEvents = True
    7.     update_Timer cell.value
    8.     cell.Interior.Color = 13434828
    9. Next cell
    10.  
    11. End Sub
    12.  
    13. Sub update_Timer(ByVal WbkName as String)
    14. Dim vc As Object
    15. Dim eend As Long
    16. Dim mystr As String, repstr As String
    17. Dim w As Workbook
    18.  
    19. Set w = Workbook.Open(WbkName)
    20.  
    21. If w.VBProject.VBComponents("XTAQ").codemodule.Find("TIMERSEC = 25", 1, 1, eend, 1, False, False, False) Then  'only do anything if that variable and value are found in the module
    22. With w.VBProject.VBComponents("XTAQ").codemodule
    23.     eend = .CountOfLines
    24.     mystr = .Lines(1, eend)
    25.     repstr = Replace(mystr, "mystr = 8", "mystr = 24")
    26.     .DeleteLines 1, eend
    27.     .InsertLines 1, repstr
    28. End With
    29. End If
    30. w.Save
    31. w.Close
    32. End Sub

    If this is still giving problems then I suggest that you liberally pepper the code with Debug.Print statements such as

    Code:
    Sub open_workbooks()
    Dim cell As Range
    Debug.Print "Starting open_workbook"
    For Each cell In Range("A2", Range("A2").End(xlDown))
        Debug.Print "Cell Address " & cell.Address & " : " & Cell.Value
        update_Timer cell.value
        Debug.Print "Finished update_timer"
        cell.Interior.Color = 13434828
        Debug.Print "Cell InteriorColor set"
    Next cell
    Debug.Print "Finished open_workbook"
    End Sub
    Signature Under Construction

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Programmatically Update Module

    i did suggest passing the workbook to the sub, but if you do as torc suggests then you need to move the application.enableevents to where the workbook is opened
    here is another option
    vb Code:
    1. Sub open_workbooks()
    2.       Dim cell As Range
    3.        
    4.       For Each cell In Range("A2", Range("A2").End(xlDown))
    5.           Application.EnableEvents = False
    6.           Set w = Workbooks.Open(cell.Value)
    7.           Application.EnableEvents = True
    8.           update_Timer w
    9.           cell.Interior.Color = 13434828
    10.       Next cell
    11.        
    12.       End Sub
    13.        
    14.       Sub update_Timer(w As Workbook)
    15.       Dim vc As Object
    16.       Dim eend As Long
    17.       Dim mystr As String, repstr As String
    18.        
    19. '      If w.VBProject.VBComponents("XTAQ").codemodule.Find("TIMERSEC = 25", 1, 1, eend, 1, False, False, False) Then  'only do anything if that variable and value are found in the module
    20.       With w.VBProject.VBComponents("module1").codemodule
    21.           eend = .CountOfLines
    22.           mystr = .Lines(1, eend)
    23.           repstr = Replace(mystr, "mystr = 8", "mystr = 24")
    24.           .DeleteLines 1, eend
    25.           .InsertLines 1, repstr & vbNewLine & "'test done"
    26.       End With
    27.       w.Save
    28. '      End If
    29.       w.Close
    30.       End Sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: Programmatically Update Module

    cheers mate, made the amendments as per your post

    excel is falling over at wb.save, only after find and replace has been done

    My security settings are ok, as I have allowed for programmatic access to vb

    the files i am trying to update, do have digital certificates but the account I am using has the certificate installed.

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Programmatically Update Module

    also you can move the enable evnts to outside the loop, so it is only run once, instead of every workbook
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width