|
-
Oct 23rd, 2006, 01:36 PM
#1
Thread Starter
Hyperactive Member
[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:
Sub Test()
Const Book = "Book5.xls"
Workbooks(Book).VBProject.VBE.CodePanes.Item(1).CodeModule.InsertLines
1, "option explicit"
Workbooks(Book).VBProject.VBE.CodePanes.Item(1).CodeModule.InsertLines
2, "SUB Test"
Workbooks(Book).VBProject.VBE.CodePanes.Item(1).CodeModule.InsertLines
3, " MsgBox ""Hello World"""
Workbooks(Book).VBProject.VBE.CodePanes.Item(1).CodeModule.InsertLines
4, "end sub"
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:
Option Explicit
Sub FillThisWorkbookCode()
AddLines 1, "Option Explicit"
AddLines 2, "' These routines run through updating the Status."
AddLines 3, "Private Sub Workbook_BeforeClose(Cancel As Boolean)"
AddLines 4, " ' Before Closing we will Update the Index with the
relevant Status, then we will"
AddLines 5, " ' Promote these out to the other sheets."
AddLines 6, " If Not ThisWorkbook.ReadOnly Then"
AddLines 7, " ThisWorkbook.Save"
AddLines 8, " End If"
AddLines 9, "End Sub ' Workbook_BeforeClose"
End Sub ' FillThisWorkbookCode
Sub AddLines(LineNumber As Long, LineText As String)
Dim mmmm As Object
' Microsoft Visual Basic for Applications Extensibility
Workbooks(TargetWbk).VBProject.VBE.CodePanes.Item(1).CodeModule.InsertLi
nes LineNumber, LineText
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 
-
Oct 23rd, 2006, 04:47 PM
#2
Re: Writing Macro to Another Spreadsheet
you are addressing codepane.item(1), which is the front codepane window, try something like
VB Code:
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
-
Oct 23rd, 2006, 05:14 PM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Oct 29th, 2006, 09:49 AM
#4
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|