dchaney
Well-known Member
- Joined
- Jun 4, 2008
- Messages
- 732
- Office Version
- 2016
- Platform
- Windows
Hello All,
I am having some issues. I have been able to create code that allows me to add VBA to the "ThisWorkbook" module a new workbook when I create it (Open, BeforeClose, SheetCalculate & SheetSelectionChange). The issue I am running into is how to add multiple procedures to a new module that I create. I think the issue I am having, is not knowing how to call the (General) object in the module (like Open, or BeforeClose on the top code).
Here is the code that works for me:
And here is the code I am having issues with:
Any assistance is appreciated...
I am having some issues. I have been able to create code that allows me to add VBA to the "ThisWorkbook" module a new workbook when I create it (Open, BeforeClose, SheetCalculate & SheetSelectionChange). The issue I am running into is how to add multiple procedures to a new module that I create. I think the issue I am having, is not knowing how to call the (General) object in the module (like Open, or BeforeClose on the top code).
Here is the code that works for me:
Code:
Dim lnStartLine As Long
Dim stCode As String, newModule
stCode = "" & vbCrLf
stCode = stCode & "Call SetTimer"
'****************************************************************************************
'ADD NEW MACRO WITH ABOVE TEXT TO NEW WORKBOOK *
'****************************************************************************************
With ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
lnStartLine = .CreateEventProc("Open", "Workbook") + 1
.InsertLines lnStartLine, stCode
End With
'********************************************************************************************
stCode = "" & vbCrLf
stCode = stCode & "Call StopTimer"
'****************************************************************************************
'ADD NEW MACRO WITH ABOVE TEXT TO NEW WORKBOOK *
'****************************************************************************************
With ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
lnStartLine = .CreateEventProc("BeforeClose", "Workbook") + 1
.InsertLines lnStartLine, stCode
End With
'********************************************************************************************
stCode = "" & vbCrLf
stCode = stCode & "Call StopTimer" & vbCrLf
stCode = stCode & "Call SetTimer"
'****************************************************************************************
'ADD NEW MACRO WITH ABOVE TEXT TO NEW WORKBOOK *
'****************************************************************************************
With ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
lnStartLine = .CreateEventProc("SheetCalculate", "Workbook") + 1
.InsertLines lnStartLine, stCode
End With
'********************************************************************************************
stCode = "" & vbCrLf
stCode = stCode & "Call StopTimer" & vbCrLf
stCode = stCode & "Call SetTimer"
'****************************************************************************************
'ADD NEW MACRO WITH ABOVE TEXT TO NEW WORKBOOK *
'****************************************************************************************
With ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
lnStartLine = .CreateEventProc("SheetSelectionChange", "Workbook") + 1
.InsertLines lnStartLine, stCode
End With
And here is the code I am having issues with:
Code:
'****************************************************************************************
'CREATE A NEW MODULE1 IN THE NEW WORKBOOK *
'****************************************************************************************
Set newModule = ActiveWorkbook.VBProject.VBComponents.Add(1)
'********************************************************************************************
stCode = "Public DownTime As Date" & vbCrLf & vbCrLf
stCode = stCode & "Sub SetTimer()" & vbCrLf & vbCrLf
stCode = stCode & "DownTime = Now + TimeValue(""00:10:00"")" & vbCrLf
stCode = stCode & "Application.OnTime EarliestTime:=DownTime, Procedure:" & _
stCode = stCode & "=""ShutDown"", Schedule:=True" & vbCrLf & vbCrLf
stCode = stCode & "End Sub" & vbCrLf
stCode = stCode & "Sub StopTimer()" & vbCrLf & vbCrLf
stCode = stCode & "On Error Resume Next" & vbCrLf
stCode = stCode & "Application.OnTime EarliestTime:=DownTime, Procedure:" & _
stCode = stCode & "=""ShutDown"", Schedule:=False" & vbCrLf & vbCrLf
stCode = stCode & "End Sub" & vbCrLf & vbCrLf
stCode = stCode & "Sub ShutDown()" & vbCrLf & vbCrLf
stCode = stCode & "ThisWorkbook.Close SaveChanges:=True" & vbCrLf & vbCrLf
stCode = stCode & "End Sub" & vbCrLf & vbCrLf
'****************************************************************************************
'ADD THE ABOVE TEXT TO THE "MODULE1" MODULE *
'****************************************************************************************
With ActiveWorkbook.VBProject.VBComponents("Module1").CodeModule
.InsertLines .CountOfLines + 1, stCode
End With
'********************************************************************************************
Any assistance is appreciated...