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.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
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?
 
el mago

What if the code wasn't indented 'properly' in the first place.:)
 
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

:)
 
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:
 
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. :)
 
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:
 
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)
 
And this bring us to the close of another meeting of the International Mutual Admiration Society. We are adjourned. :LOL:
 

Forum statistics

Threads
1,214,832
Messages
6,121,843
Members
449,051
Latest member
excelquestion515

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