![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Set Variable to Nothing (VBA for Excel)
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Set Variable to Nothing (VBA for Excel)
- This topic has 18 replies, 6 voices, and was last updated 20 years, 1 month ago.
Viewing 0 reply threadsAuthorReplies-
WSHansV
AskWoody LoungerMay 3, 2005 at 7:16 am #944936Object variables (the ones you assign using Set) are stored in a very different way from “simple” variables such as Integers or Booleans. Windows has to reserve space for all their properties etc.
VBA performs the necessary garbage collection at the end of a procedure/function, but if an unhandled error occurs, this may fail, and the objects will remain in memory. I have seen examples in Access where code didn’t work as expected because objects had not been not set to Nothing previously. So it is conventional wisdom to add error handling to all procedures / functions that use object variables, and to make sure that all object variables are set to Nothing even if an error occurs. -
WSRudi
AskWoody LoungerMay 3, 2005 at 8:15 am #944950That does make sense. I can understand that that this would be conventional in an error handler, but Steve (and many others) use it outside of an error handler…(see link in previous post!). Why use it like this when End Sub will clear all variables in the very next line…(Is this just a case of habbit (being a good one), or being over cautious??). These lines will never be reached if an error occured further up the code??
PS: Just querying this to get clarity…I’m not arguing the point!!
-
WSHansV
AskWoody Lounger -
WSRudi
AskWoody Lounger -
WSHansV
AskWoody LoungerMay 3, 2005 at 9:02 am #944956 -
WSRudi
AskWoody LoungerMay 3, 2005 at 9:58 am #944967 -
byteme
AskWoody PlusMay 3, 2005 at 4:01 pm #945088I admit to being pretty fastidious myself about setting object variables to Nothing. But in case anyone’s interested, here’s a cutely-titled contrarian view from VB guru Mark Hurst: Much Ado About Nothing.
-
WSchrisgreaves
AskWoody Lounger -
byteme
AskWoody Plus -
WSRudi
AskWoody Lounger
-
-
-
-
WSchrisgreaves
AskWoody LoungerMay 3, 2005 at 11:11 am #944991> I have seen examples in Access where code didn’t work as expected because objects had not been not set to Nothing previously.
And I have seen examples in Fortran/COBOL/ALGOL/APL/ ….where code didn’t work as expected because STUFF had not been not set to the-appropriate-null-value previously.
I think it’s a good programming habit to initialize every variable; execution time is negligible.
I’m somewhat doubtful of doing garbage collection for the run-time routines.
I am concerned about readablity of code, because I believe that it’s the cost in human time that impacts development projects. Too many lines of housekeeping can distract my attention from the real purpose of the code.I suspect we will find a balance, that there are procedures which can be shown NOT to need initialization to null, and can be shown NOT to need finalization to null. Presumably only if a procedure can be guaranteed to execute to completion, or noty be interruptible, can we abandon garbage collection.
That said, I just KNOW I’m going to run into problems if I don’t take care with me new objects that are non-visible applications!
-
WSRudi
AskWoody Lounger -
WSchrisgreaves
AskWoody LoungerMay 3, 2005 at 12:08 pm #945008>but what the heck did you say?
Sorry, I’ve still had only one coffee,1) Too much clutter in housekeeping code can distract the reader, and that makes for problems (clean concise readable code is good).
2) Even so, there are penalties when one creates application objects (that sit there, visible or not on the Windows task bar) and doesn’t get rid of them when they are done.I think all in all :
I just can’t see the economic($) sense in taking care of the garden-variety of variables – Long, String, Boolean etc
I can see sense in tidying up after the big boys such as Application Objects
I’m ambivalent on intermediate things such as Structures, Dictionaries, Fields, Tables etc. -
WScharlotte
AskWoody LoungerMay 3, 2005 at 12:23 pm #945014The rule of thumb is that any object variable (initialized with the Set keyword) needs to be destroyed. Not doing so can result in the infamous memory leaks from your application. While those objects are *supposed* to be destroyed when they go out of scope, it doesn’t always happen. Then you get weird behavior like an application refusing to shut down entirely.
And BTW, concise and readable are sometimes mutually exclusive.
-
WSchrisgreaves
AskWoody LoungerMay 3, 2005 at 12:44 pm #945023 -
WSAlanMiller
AskWoody LoungerMay 3, 2005 at 2:59 pm #945083I’m not sure how it works in VB, but in C++ it quickly becomes very clear why it’s necessary to properly destroy objects that contain pointers to other objects. Without explicitly writing a correct destructor (roughly equivalent to setting object references to Nothing) it’s quite easy to end up with a pointer refering to an object that no longer exists -> crash.
Alan
-
WSchrisgreaves
AskWoody LoungerMay 3, 2005 at 3:33 pm #945092> it’s necessary to properly destroy objects
I agree. I think confusion reigns in VBA because of aberrations in MSot’s implemennation.
The referenced article (below) says that it’s not necessary to set to Nothing; VBE does it for you. Trouble is VBE doesn’t always do it properly, so some of us decide to Set to Nothing on principle.
Explict Set Nothings are guaranteed to work; in VBA they ought not to be necessary.
-
WScharlotte
AskWoody Lounger
-
-
-
-
Viewing 0 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
How start headers and page numbers on page 3?
by
Davidhs
2 hours, 34 minutes ago -
Attack on LexisNexis Risk Solutions exposes data on 300k +
by
Nibbled To Death By Ducks
4 minutes ago -
Windows 11 Insider Preview build 26200.5622 released to DEV
by
joep517
11 hours, 15 minutes ago -
Windows 11 Insider Preview build 26120.4230 (24H2) released to BETA
by
joep517
11 hours, 16 minutes ago -
MS Excel 2019 Now Prompts to Back Up With OneDrive
by
lmacri
58 minutes ago -
Firefox 139
by
Charlie
13 hours, 39 minutes ago -
Who knows what?
by
Will Fastie
6 hours, 22 minutes ago -
My top ten underappreciated features in Office
by
Peter Deegan
12 hours ago -
WAU Manager — It’s your computer, you are in charge!
by
Deanna McElveen
6 hours, 24 minutes ago -
Misbehaving devices
by
Susan Bradley
14 hours, 8 minutes ago -
.NET 8.0 Desktop Runtime (v8.0.16) – Windows x86 Installer
by
WSmeyerbos
1 day, 17 hours ago -
Neowin poll : What do you plan to do on Windows 10 EOS
by
Alex5723
1 hour, 58 minutes ago -
May 31, 2025—KB5062170 (OS Builds 22621.5415 and 22631.5415 Out-of-band
by
Alex5723
1 day, 16 hours ago -
Discover the Best AI Tools for Everything
by
Alex5723
15 hours, 43 minutes ago -
Edge Seems To Be Gaining Weight
by
bbearren
1 day, 6 hours ago -
Rufus is available from the MSFT Store
by
PL1
1 day, 14 hours ago -
Microsoft : Ending USB-C® Port Confusion
by
Alex5723
2 days, 17 hours ago -
KB5061768 update for Intel vPro processor
by
drmark
17 hours, 30 minutes ago -
Outlook 365 classic has exhausted all shared resources
by
drmark
16 hours, 12 minutes ago -
My Simple Word 2010 Macro Is Not Working
by
mbennett555
2 days, 13 hours ago -
Office gets current release
by
Susan Bradley
2 days, 16 hours ago -
FBI: Still Using One of These Old Routers? It’s Vulnerable to Hackers
by
Alex5723
4 days, 6 hours ago -
Windows AI Local Only no NPU required!
by
RetiredGeek
3 days, 14 hours ago -
Stop the OneDrive defaults
by
CWBillow
4 days, 6 hours ago -
Windows 11 Insider Preview build 27868 released to Canary
by
joep517
4 days, 16 hours ago -
X Suspends Encrypted DMs
by
Alex5723
4 days, 19 hours ago -
WSJ : My Robot and Me AI generated movie
by
Alex5723
4 days, 19 hours ago -
Botnet hacks 9,000+ ASUS routers to add persistent SSH backdoor
by
Alex5723
4 days, 19 hours ago -
OpenAI model sabotages shutdown code
by
Cybertooth
4 days, 20 hours ago -
Backup and access old e-mails after company e-mail address is terminated
by
M W Leijendekker
4 days, 8 hours ago
Recent blog posts
Key Links
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.