View Single Post
  #15   Report Post  
Posted to microsoft.public.excel.programming
Arif Ali[_2_] Arif Ali[_2_] is offline
external usenet poster
 
Posts: 16
Default VBAProjects remain in memory after workbook is closed

Guys,

Clearly there are some basic rules I have been violating, but let's simplify
things. Take this routine for example - I changed the name from
"InitQuoteSummaryWindow" to "DisplayQuoteSummaryListbox". This code exists
in the Main Workbook.

By the way there is NO code in any other workbook except this one. However
there is code elsewhere in this workbook, behind UserForm1. UserForm1 is a
toolbar with lot of different buttons on it.

When the User clicks a button called "View Quote Summary", the button_click
routine (located behind UserForm1 calls
Thisworkbook.DisplayQuoteSummaryListbox. Subsequent to that call is the line
frmQuoteSummary.Show. That's the only code in the button_Click routine for
btnViewQuoteSummary.

The sole purpose of this code is to:

1. Open the QuoteSummary.XLS workbook.
2. Populate a Listbox with its contents (this all works by the way)
3. Close the workbook.

For the time being, the purpose of this listbox is so the user can simply
view all the quotes that were generated in the past. Later I will enhance
this to make it possible to open a previously saved quote by double-clicking
within the listbox. But for right now it is just a viewer. I recognize that
the ListBox lives on after QuoteSummary.xls is closed, but I do not believe
that the listbox retains any connection to the workbook. Again it is only a
viewer of historical information. For the time being, all a user can do is
view it and close it when done.

One other note: in order to reduce accidental mis-references, I have
switched to 99% local references rather than global references. The only
Global reference I still have is:

Public Quoter as Excel.Workbook ' Referring to the main workbook

This mean that aside from the above, within each routine, all references are
created and assigned at the beginning of the sub and set to nothing at the
end. As far as I'm concerned the following routine is a very simple routine,
but I have two problems:

1. After the first time it is run, subsequent runs leave a copy of the
VBAProject QuoteSummary1 in memory. If it runs 4 times, there are 3 copies
of the VBAProject in memory!

2. There are no click event associated with the listbox. However, if I
click on an item in the listbox, I get the message "There is not enough
memory to complete this operation". (What operation?!)

Public Sub DisplayQuoteSummaryListbox()
Dim QS As Excel.Workbook
Dim i As Integer
Application.ScreenUpdating = False

Set QS = Workbooks.Add(QSPath & "QuoteSummary.xls")
QS.Sheets("Quotes").Activate
QS.ActiveSheet.Range("A1").Select
i = 0
Do While ActiveCell.Value < ""
i = i + 1
ActiveCell.Offset(1, 0).Select
Loop

frmQuoteSummary.ListBox1.Font.Name = "Arial"
frmQuoteSummary.ListBox1.Font.Size = 10
frmQuoteSummary.ListBox1.ColumnCount = 6
QS.Activate
frmQuoteSummary.ListBox1.ColumnHeads = False
frmQuoteSummary.ListBox1.RowSource = "a1:f" & CStr(i)
frmQuoteSummary.ListBox1.MultiSelect = fmMultiSelectSingle
frmQuoteSummary.ListBox1.ColumnWidths = "72;108;108;108;96;96"
frmQuoteSummary.ListBox1.TextAlign = fmTextAlignLeft

Application.ScreenUpdating = True

QS.Close
Set QS = Nothing

End Sub