Custom CSS tags for displaying well-formed VBA code

So you might be wondering, what does VBA have to do with a web technology like CSS? If you are like me and you hand-code your website, you might find it difficult to display VBA code properly. Unfortunately, I can't seem to locate a copy of zHTML, and the other VBA-to-HTML converters I tried just don't seem to satisfy.

Since my website is formatted using CSS, I did a bit of digging and found I could create my own HTML tags. I was already using the PRE and TEXTAREA tags to format snippets of VBA code, but this technique allows you to create custom tags that make it simple to roll your own webpages. So I went ahead and wrote some CSS to format the four most commonly posted types of VBA code: Normal, Selection, Comment and Keyword Text. Here it is:

.vba
{ color: #000000;
background: #FFFFFF;
padding: 10px 10px 10px 10px;
font-size: 100%
}
.selected
{ color: #FFFFFF;
background: #330099;
}
.comment
{ color: #339933;
background: #FFFFFF;
}
.keyword
{ color: #330099;
background: #FFFFFF;
}

Paste the above code into the .css file that defines your site's layout. You can fiddle with the background color to make it suit your site better. Now you can wrap your VBA code in tags like this:

Sub fixformulas()
'
' if you have some formulas with a ' or some other
' character in the beginning'
Dim cellAs Excel.Range
For Each cell 
In  Selection
    cell = "=" & Right(cell, Len(cell) - 1)
Next cell
End Sub

And it will display like this:

Sub fixformulas()
'
' if you have some formulas with a ' or some other 
' character in the beginning
'
Dim cell As  Excel.Range
For Each cell In Selection
    cell = "=" & Right(cell, Len(cell) - 1)
Next cell
End Sub

The PRE tag wraps the area in the "vba" tag, which is the default black-on-white text used to display most VBA code (if you haven't fiddled with the default settings in the VBE). So any code that isn't wrapped in a span tag will automatically take on the default color, which is the intuitive way the VBE does it.

Instead of using the deprecated FONT tags or trying to guess the hex values for colors every time you want to use them, you can use friendly words like "keyword" or "comment" in your SPAN tags and the code will be colored accordingly. The PRE tag will also make sure the code is indented properly!

Eventually I'll convert all the existing code to this format, and all future code posted on the blog and the website will be displayed using these custom made CSS class tags.

(ps- the Outlook event code I promised last week is coming up in the next day or two. I was away this week so I have a lot of catching up to do.)

Enjoy,
JP

Related Articles:

About JP

I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space to learn more about VBA. Keep Reading »

Share This Article:

Share and bookmark this articledelicious buttonfacebook buttonlinkedin buttonstumbleupon buttontwitter button

comment bubble 2 Comments:

  1. Jon Peltier writes:

    JP -

    Thanks for this. I've been using a WordPress plugin for code on my blog, but I haven't really cared much for it. At times I'd played with styles in my web site's html, but this gives me the guidance that I need.

    Jon Peltier (the other JP)

  2. JP writes:

    No problem JP (LOL!), you are lucky that WordPress has something for posting code. But I'm not using this on my blog, it's for the site. For the blog I still do it the old fashioned way, using SPAN tags to define the color scheme for each bit of VBA. I might try to edit the CSS layout for the Blogger template, though. That would be a treat.

    Thx,
    JP

Comments on this article are closed. Why?

Site last updated: February 12, 2012