Which Is Better Coding?

Empty is a value reserved to Variants.

Assigned to a numeric variable, it gets converted to zero. It doesn't release any storage in either case, unless assigned to a variant array. Variables (scalers, scalar arrays, objects, ...) are released when they go out of scope.
 
Last edited:

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I agree with all the points about Option Explicit, Declaring Variables, indenting code, commenting code, etc.

By no means do I consider myself a programmer, but as a member of this board I have learned some invaluable tips and not one them I can think of which would cause any extra ordinary time in coding practices.

When I was just starting out learning how to use code, I would get some code from somebody who did not use Option Explicit, but since my default was Option Explicit, I spent time scratching my head trying to declare all the variable which were left out. I felt this was by far a better way to understand the code than just removing Option Explicit.

Just today I used a little snippet of code I have saved and realized if I updated the code just slightly it would be a little more efficient in the updating department. Probably something else could be done, but this gets me a little further down the road. With the before code, if you change the column you have to update it three times where in the after just once. May seem minuscule in the big picture, but every bit helps.

Before:
Code:
Sub concat1()
    Dim LR As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
    Range("B1").Value = Join(Application.Transpose(Range("A1:A" & LR)), ", ")
End Sub

After:
Code:
Sub xpos()
    Dim LR As Long
    Const ColLet As String = "A"
    Const SR As Long = 1
    Dim rngDst As Range
    Set rngDst = Range("B1")
    LR = Range(ColLet & Rows.Count).End(xlUp).Row
    rngDst = Join(Application.Transpose(Range(Cells(SR, ColLet), Cells(LR, ColLet))), ", ")
End Sub

FWIW...my 2-cents... :)

BTW, using the Const variable in the code is by way of Rick Rothstein and I thank him and the many others on this Forum for sharing their wonderful talents and experience.
 
Last edited:
BTW, using the Const variable in the code is by way of Rick Rothstein and I thank him and the many others on this Forum for sharing their wonderful talents and experience.

FYI, Const is, by definition, not a variable. ;)
 
diddi, you are using this right? http://www.oaltd.co.uk/Indenter/indentpage.asp (I guess if you can, due to corp. policies etc...)
Helps me find missing "End __'s" and the like.

hi arkusM, no i dont use any utilities for indenting. its just a way i find that works for me to match up all the nesting. if i get to the end of a code fragment and things dont line up i can be pretty sure that i have missed an End If or something. i guess it forces me to work in small logical blocks and not get too unruly.

i did try an addin once for indenting but i found it more of a pest than a help. cant remember that name of it now. lol

oh, and a thought about variable names, for what its worth: i often use an uppercase char when i declare my variable names. eg MyVar , the benefit being that when i have an attack of digital dyslexia, the variable doesnt autocapitalise (eg myvra) and then i know i need to fix it up.




good to see some lively discussion in the lounge!
 
Last edited:
diddi

You really should try SmartIndenter which I think is the add-in at the end of the link.

It's certainly not a pest - right click, select what you want to indent(Procedure, Project, Module) and that's it.

You can also set various options, eg align Dim's in a column (which I've never used)

It's certainly handy when copying data from here to find the source of that elusive Next before For error and the like.:)

Oh, forgot to add - when it's installed it's installed in all of Office.
 
This thread is pretty mature by now and I reckon most POV's have been covered. So, Peter, if you're still monitoring this, I admit that when I read your question, I immediately made several assumptions and I confess I'm curious to know whether I'm right...

How long have you been writing code? Have you ever had to go back and try and decipher code you wrote five, ten, fifteen or even twenty years ago?

Have you ever had to try and unravel mission critical code that suddenly quit working after some software upgrade or hardware change and the code was poorly documented (if it was documented at all)? Or had to sift through someone else's code and try to comprehend it when it was written with all one-letter variable names? All while you're getting hourly e-mails from senior management asking "how's it going"?

Have you ever had to step through hundreds of lines of code hunting down some "magic value" that is embedded deep in code in several different procedures because the programmer failed to use a module-level or global constant? (Which, of course, is doubly sad when you are the nitwit who wrote the code ten years back, before you knew better.:rolleyes:)
 
Last edited:
Have you ever had to step through hundred of lines of code hunting down some "magic value" that is embedded deep in code in several different procedures because the programmer failed to use a module-level or global constant? (Which, of course, is doubly sad when you are the nitwit who wrote the code ten years back, before you knew better.:rolleyes:)

Doh, busted... This happens to me, I am glad I have started to be better thanks to the kind folks here...

diddi, check out smart indenter, time saver, not a crutch, well ok it could be.... but I can affirm what Norie said.
 
I've only just started using the Smart Indenter after ages trying to persuade IT to let me install it at work and have to admit it's very useful and saves a lot of time moving code around. Even though I don't think it says on the website it works fine in Excel 2007/2010 as well.

Dom
 
Ditto on the Smart Indenter - cannot do without it. I've now got the keystrokes down to a split second fine art of indent project followed by Debug, Compile everytime I'm ready to save. It's a shame that Mr Bullen seems to have not added anything new to his site for a while as he's done some brilliant work although I guess a man of his skill would be plenty busy. <a href="http://www.oaltd.co.uk/DLCount/DLCount.asp?file=FormFun.zip">FormFun.zip</a> is another of my favourites of his.
 

Forum statistics

Threads
1,223,757
Messages
6,174,331
Members
452,555
Latest member
colc007

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