Cells(1,1) vs Range("A1")

As far as setting objects equal to Nothing when you're code routine is nearing completion, I used to do this as well as I thought it "cleaned up" my code. In most languages (that I've seen) this is a good thing. Although in VBA it is not needed, as the objects are destroyed when the routine completes and loses focus anyway. So I stopped writing it out as it was being done in the next operation anyway (exiting the routine).


Take care all!

I disagree with all those who purport to that. i.e. setting objects = nothing is NOT required. I beleive it is good pratice to Release/Destroy objects on closing, in order of creation FCFD (First created First Destroyed) especially when automating COM objects eg DAO and ADO. Yes COM objects have calls to AddRef and Release on objects (it actually keeps count of refs set), this is a part of the COM contract. Even if the the documentation says it is destroyed upon closing I would still do this.

The cost to add just a few lines of code is nothing, as opposed to the cost of a left over instance(s) in the ROT or ghost threads with there subsequent drain on resources (memory leaks and process time)

The same is true of API programing where you must use the corresponding Destroy/Release/Delete API
eg
LoadBitmapA > DeleteObject
CreateBitmap > DeleteObject
CreateSolidBrush > DeleteObject
CreateCursor > DestroyCursor
CreateCompatibleDC > DeleteDC
GetDC > ReleaseDC
CreateFontA > DeleteObject
CreateIcon > DestroyIcon
ExtractIconA > DestroyIcon
CreateMetaFileA > CloseMetaFile
CopyMetaFileA > DeleteMetaFile
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi Ivan, I use the practive you are recomending. But just to play devil's advocate, shouldn't the Garbage collector automaticlly dispose of those objects as they fall out of scope at the end of the sub?
 
1. @ d3p2j5b: I do the same thing, but I stick the column defs at the very top of the module inside an ENUM/END ENUM. Makes them easier to spot. And for essentially the same reason. But in my case it's usually that my code is working with mainframe extracts and my boss has had IS add a column to the report w/o telling anybody but IS and one day code that has worked well for years is suddenly way the he... off in left field somewhere and I'm trying to figure out what happened.

2. @ Ivan: FCFD? I had always assumed that I needed to do FCLD. For example, if one creates a WB object, then a WS object, then a Range object. If one kills the WB first, would not the WS & Range still have some type of "implied" connection to the WB that might result in it hanging open? I assumed it would be better to kill the child object first and work my way up the container structure. I assume FCFD is the standard practice then?
 

Forum statistics

Threads
1,213,524
Messages
6,114,117
Members
448,549
Latest member
brianhfield

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top