Results 1 to 4 of 4

Thread: [RESOLVED] Writing Macro to Another Spreadsheet

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Resolved [RESOLVED] Writing Macro to Another Spreadsheet

    I have a spreadsheet which is run each day, putting its results into a
    monthly results file, Thus the results for October will be in a file
    Results200610October.xls

    The results are then actioned by a user, with details being updated in
    the results spreadsheet.

    I need to run a macro in the spreadsheet when a user who has it open,
    closes it, provided it is not ReadOnly.

    I had this in the ThisWorkbook code pane of a spreadsheet called Book4.xls
    I also had Book5.xls open. This worked fine and I was able to step
    through using the Debugger easily, and it worked.
    I have Microsoft Visual Basic for Applications Extensibility reference
    Library selected.

    VB Code:
    1. Sub Test()
    2. Const Book = "Book5.xls"
    3.  
    4. Workbooks(Book).VBProject.VBE.CodePanes.Item(1).CodeModule.InsertLines
    5. 1, "option explicit"
    6.  
    7. Workbooks(Book).VBProject.VBE.CodePanes.Item(1).CodeModule.InsertLines
    8. 2, "SUB Test"
    9.  
    10. Workbooks(Book).VBProject.VBE.CodePanes.Item(1).CodeModule.InsertLines
    11. 3, "   MsgBox ""Hello World"""
    12.  
    13. Workbooks(Book).VBProject.VBE.CodePanes.Item(1).CodeModule.InsertLines
    14. 4, "end sub"
    15. End Sub

    Then I converted it into a separate module in my App, Please note that
    TargetWbk is the filename of the Spreadsheet I am creating. It is declared
    as a Global and when I debug has the correct filename in.

    VB Code:
    1. Option Explicit
    2. Sub FillThisWorkbookCode()
    3.     AddLines 1, "Option Explicit"
    4.     AddLines 2, "' These routines run through updating the Status."
    5.     AddLines 3, "Private Sub Workbook_BeforeClose(Cancel As Boolean)"
    6.     AddLines 4, "    ' Before Closing we will Update the Index with the
    7. relevant Status, then we will"
    8.     AddLines 5, "    ' Promote these out to the other sheets."
    9.     AddLines 6, "    If Not ThisWorkbook.ReadOnly Then"
    10.     AddLines 7, "        ThisWorkbook.Save"
    11.     AddLines 8, "    End If"
    12.     AddLines 9, "End Sub ' Workbook_BeforeClose"
    13. End Sub ' FillThisWorkbookCode
    14.  
    15. Sub AddLines(LineNumber As Long, LineText As String)
    16. Dim mmmm As Object
    17.     ' Microsoft Visual Basic for Applications Extensibility
    18.  
    19. Workbooks(TargetWbk).VBProject.VBE.CodePanes.Item(1).CodeModule.InsertLi
    20. nes LineNumber, LineText
    21. End Sub

    The problem is that the above code is not adding the Macro to the
    ThisWorkbook code of TargetWbk, but to the Actual Module it self in front of
    the code itself.

    In addition it is not letting me Break to examine the program flow in
    the debugger.

    Can anyone advise me as to what is causing these problems.
    Signature Under Construction

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

    Re: Writing Macro to Another Spreadsheet

    you are addressing codepane.item(1), which is the front codepane window, try something like
    VB Code:
    1. Application.VBE.VBProjects(1).VBComponents("Module7").CodeModule.DeleteLines 7
    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
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing Macro to Another Spreadsheet

    For more examples on writting VBA code via VBA/VB 6 see my Code Bank thread - http://vbforums.com/showthread.php?t=313861
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Resolved Re: Writing Macro to Another Spreadsheet

    Thanks westconn1,

    It works wonderfully now.

    Apologies for not replying to your post sooner, disk problems meant I was unable to test until Friday.

    Thanks for the link RobDog, proved very informative.
    Signature Under Construction

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