Why use code tags?

the magician

Active Member
Joined
Nov 9, 2006
Messages
496
Why use code tags? Using code tags makes it much easier to read your code, because without them, your code looks like this:

Sub SplitNameWithInstrrevJoinJr()
Dim c As Range, i As Integer
Dim first As String, last As String
For Each c In Range("A1:A" & Range("A65536").End(xlUp).Row)
If Trim(c.Offset(, 3)) = "III" Or Trim(c.Offset(, 3)) = "Jr." Then
i = InStrRev(c, " ")
If i > 0 Then
first = Trim(Left(c, i))
last = Trim(Mid(c, i)) & " " & Trim(c.Offset(, 3))
c = first
c.Offset(, 3) = last
Else
c = first & " " & last
End If
End If
Next
End Sub

YUCK! :p

With code tags, it looks like this:

Code:
Sub SplitNameWithInstrrevJoinJr()
Dim c As Range, i As Integer
Dim first As String, last As String
For Each c In Range("A1:A" & Range("A65536").End(xlUp).Row)
    If Trim(c.Offset(, 3)) = "III" Or Trim(c.Offset(, 3)) = "Jr." Then
        i = InStrRev(c, " ")
        If i > 0 Then
            first = Trim(Left(c, i))
            last = Trim(Mid(c, i)) & " " & Trim(c.Offset(, 3))
            c = first
            c.Offset(, 3) = last
        Else
            c = first & " " & last
        End If
    End If
Next
End Sub
Much better, don't you agree????:biggrin:

Using code tags is easy. Either paste your code into the message window, then highlight it (drag-select it) and click the "Code" button (you'll see it just above the "Normal" drop-down for Font Settings). Or if you want to, you can just type in a bracket, the word code, and another bracket at the beginning of the your code sample, and then a bracket, /code, and a closing bracket at the end. Like this:
Code:
[code]Your code here
[/code]

Now the gang will take over the discussion from here with some lively banter and a few more examples of how and how not to post code, depending on your musicianship.
 

Some videos you may like

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

brian.wethington

Well-known Member
Joined
Jul 20, 2006
Messages
1,739
Ah Come on mage.... you know you really enjoy looking at code and trying to distinguish what the heck a person was trying to do when he/she didn't provide comments let alone readable code. Then you come to find out that because they did not do this they simply forgot a End if.

Man, that is the good life, don't you know?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,654
Office Version
365
Platform
Windows
el mago

What if the code wasn't indented 'properly' in the first place.:)
 

Von Pookie

MrExcel MVP
Joined
Feb 17, 2002
Messages
13,686
Then at least if it is within the code tags we can tell where it begins/ends.

Really, though: Note how he has this thread linked in his signature? This thread is basically boilerplate moved from the Test forum so it won't be deleted when we turn on the auto-purging.

For Erik:
'boilerplate': newspaper (and now information technology) slang for "unit of writing that can be used over and over without change..."
http://dictionary.reference.com/search?q=boilerplate

:)
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Kristy :confused: :LOL:

this was how I addressed people sometimes about codetags: now I'll use this thread too
thanks for starting it, magician

The mind of an Excel-lover is full of structure. It loves reading code which is indented: analysing is quicker: so every effort can go to the problem itself...

To let the code stay indented do the following.
Click "reply"
write your message
select your code
click the "Code"-button
(or Click code button, paste your code, click "Code" button again.)
Submit


A real relief for those who try to help you !

WITHOUT
Sub Macro4()
For i = 1 To 3
For j = 1 To 3
m = m & i & j & Chr(10)
Next j
Next i
MsgBox m
End Sub

WITH
Code:
Sub Macro4()
    For i = 1 To 3
        For j = 1 To 3
        m = m & i & j & Chr(10)
        Next j
    Next i
MsgBox m
End Sub
Code:
kind regards,
:wink:
 

NateO

Legend
Joined
Feb 17, 2002
Messages
9,700
Erik,

You forgot to tab the following line of code:

Code:
m = m & i & j & Chr(10)
Inside the 2nd For/Next Loop, eh?

I'd also recommend replacing Chr(10) with vbLf. ;)

And, as we know, i & j should be Long variables, while m should be a String...

What kind of an example are we cooking up here?! :p :LOL:


  • Edited by Nate: I'm just joking around here.

    Typically people tab the code nested in their loops, and I find it ironic that we use the Code Tags to demonstrate conventionally formatted code, which is a good thing... But we do not actually write the code in a conventionally formatted manner...

    No harm intended. :)
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Nate,

this is the way I like to tab: also no harm intended :biggrin:
but I'm not in any way a "real" programmer
I just love to play keys* and invent

very kind regards and with greatest respect for your wonderful moderatorship,
(I mean it)
Erik

*most of them are located on my piano
next concert here in town March 11 at 11 AM
WELCOME :wink:
 

NateO

Legend
Joined
Feb 17, 2002
Messages
9,700
Hello again,
this is the way I like to tab: also no harm intended :biggrin:
Ah, ok, Erik. I thought maybe it was inadvertent, so I thought I might tease you a bit. :)

We all have somewhat differing styles, and that's ok... I don't know if I'm a *real* programmer, either... I'd be hesitant to think of myself as a fake programmer, though... :confused:

I always use Option Explicit, so I have to declare my variables, and I always tab code that is housed within a loop, so my version of your procedure might look like:

Code:
Option Explicit

Sub foo()
Dim i As Long, j As Long, m As String
For i = 1 To 3
    For j = 1 To 3
        Let m = m & i & j & vbLf
    Next j
Next i
MsgBox m
End Sub
And of course, there's other ways to go about the very same task, as usual. Here's an example of a one-liner that sets out to accomplish the same goal:

Code:
Sub bar()
MsgBox Join(Evaluate("Transpose((Row(1:3)-1)*10+11&Char(10)" & _
    "&(Row(1:3)-1)*10+12&Char(10)" & _
    "&(Row(1:3)-1)*10+13)"), vbLf)
End Sub
Either way... I find that the code tags assist in reading formatting code immensely. ;)

And thank you, Erik, for the nice sentiments. (y)
 

the magician

Active Member
Joined
Nov 9, 2006
Messages
496
And this bring us to the close of another meeting of the International Mutual Admiration Society. We are adjourned. :LOL:
 

Watch MrExcel Video

Forum statistics

Threads
1,102,545
Messages
5,487,494
Members
407,603
Latest member
jortronm

This Week's Hot Topics

Top