# Why use code tags?



## the magician (Feb 21, 2007)

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!  

With code tags, it 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
```

Much better, don't you agree???? 

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]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.


----------



## brian.wethington (Feb 21, 2007)

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 (Feb 21, 2007)

el mago

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


----------



## Von Pookie (Feb 21, 2007)

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 (Feb 21, 2007)

Kristy    

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

```
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
```


```
kind regards,
```



> Erik


 :wink:


----------



## NateO (Feb 21, 2007)

Erik,

You forgot to tab the following line of 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?!  



*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 (Feb 22, 2007)

Nate,

this is the way I like to tab: also no harm intended   
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 (Feb 22, 2007)

Hello again,


> this is the way I like to tab: also no harm intended


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...  

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:


```
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:


```
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.


----------



## ExcelChampion (Feb 23, 2007)

While were at it....Smart Indenter is, in my opinion, one of the most useful add ins I've ever come across...I wouldn't want to live with out it:-

http://www.oaltd.co.uk/Indenter/Default.htm

Many thanks to Office Automation Ltd.


----------



## the magician (Feb 24, 2007)

And this bring us to the close of another meeting of the International Mutual Admiration Society.  We are adjourned.


----------

