I'm trying to create a subroutine in the Excel 2002 VB Editor to automatically remove the module once all the script has finished running (trying to automate a daily process using Excel Macros that run when the worksheet is opened). My goal is to save the resulting sheet as a new sheet, and not have the macros follow it, thus bringing up the "Enable Macros/Disable Macros" warning that's featured in Office XP.
My Code is this:
Private Sub Auto_Open()
' MsgBox "Running Workbook_Open . . ."
Dim sTest As String
sTest = Range("A2").Value
If sTest = "" Then
Private Sub dataExtract()
' MsgBox "Running dataExtract . . ."
' Create a connection object.
Dim cnPubs As ADODB.Connection
Set cnPubs = New ADODB.Connection
' Provide the connection string.
Dim strConn As String
'Use the SQL Server OLE DB Provider.
strConn = "PROVIDER=SQLOLEDB;"
'Connect to the Pubs database on the local server.
strConn = strConn & "DATA SOURCE=[Server];INITIAL CATALOG=[pubs];"
'Use an integrated login.
strConn = strConn & " INTEGRATED SECURITY=sspi;"
'Now open the connection.
' Create a recordset object.
Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset
Dim dtYesterday As String
Dim dtToday As String
dtToday = DatePart("w", Date)
If dtToday = "2" Then
dtYesterday = Date - 3
dtYesterday = Date - 1
' MsgBox dtYesterday
' Assign the Connection object.
.ActiveConnection = cnPubs
' Extract the required records.
.Open "Select e.programno, e.programdate, c.LName, c.FName, c.PaxId from EnrollmentsHist e, Customer c where (e.XActDate >= '" & dtYesterday & "') AND (c.PaxID = e.PaxID) AND (e.LastXAct = 'C') AND (e.ClientID in ('eh', 'ehd', 'rs'))"
' Copy the records into cell A1 on Sheet1.
' Tidy up
Set rsPubs = Nothing
Set cnPubs = Nothing
Private Sub sortData()
Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Key2:=Range("B2") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
MsgBox "Hi, I will delete myself "
I don't get any errors, and once the marcos are done running the code window in the VB Editor closes, but the module isn't actually removed. I've also tried these sets of code in Sub DeleteThisModule(), but had the same lack of results:
Dim VBComp As VBIDE.VBComponent
Dim VBComps As VBIDE.VBComponents
Set VBComps = ActiveWorkbook.VBProject.V
For Each VBComp In VBComps
Select Case VBComp.Type
Case vbext_ct_StdModule, vbext_ct_MSForm, vbext_ct_ClassModule
.DeleteLines 1, .CountOfLines
Dim vbCom As Object
Set vbCom = Application.VBE.ActiveVBPr
vbCom.Remove VBComponent:= _
I have the "Tools > Macro > Security..." "Trust access to Visual Basic Project" checked already.
I have MS VB for Applications Extensibility 5.3, MS ActiveX Data Objects Recordset 2.8 Library, MS ActiveX Data Objects 2.8 Library, XLODBC.XLA, MS Office 10.0 Object Library, OLD Automation, MS Excel 10.0 Object Library, and Visual Basic for Applications all loaded from Tools > References in MS VB Editor.
500 Points because a quick solution would be best, also this seems difficult enough to merit them.