Dick very recently put up a function that takes an Excel range and whips up some HTML to create a table. I loved that idea, and I asked if I could flesh it out. With Dick’s green light, I created a Sub() that captures each cell’s font family, font style, and font color, as well as the cell’s alignment and background color. It retains Dick’s option to use headers or not.
The sub spits the table to the clipboard. To get it there, in the VBE use Tools/References and check the Microsoft Forms 2.0 Object Library.
The table is a mixture of HTML and deprecated HTML (I’m not a purist.) I played with capturing the font size, but never liked how it came out so I commented it out. In creating HTML or CSS, many times you need to uses double-quotes (“) around the parameters. You can get by without it if the parameters are a single word, Arial for instance, but not for Times New Roman. Getting double-quotes in a text string requires you to escape them with another set of double quotes, creating double double-quotes (“”) and my eyes start to cross. I trick I use is to define a string*1 constant DQ equal to double double double-quotes (“”””). And then where I want quotes to appear in the HTML or CSS, I just concatenate in DQ. I used it throughout the Sub().
Const DQ As String * 1 = “””” ‘double double double-quotes
Dim DataObj As New MSForms.DataObject
‘Check VBE Tools/References Microsoft Forms 2.0 Object Library
Dim rInput As Range
Dim rRow As Range
Dim rCell As Range
Dim sReturn As String
Dim TextAlign As String
Dim VertAlign As String
Dim BgColor As String
Dim FontColor As String
Dim FontFace As String
Dim CellContents As String
Dim UseHeaders As Long
Dim FontSize As Long
Dim R As Long, C As Long
Dim Red As String
Dim Green As String
Dim Blue As String
Dim TEMP As Variant
Set rInput = Selection
R = rInput.Rows.Count
C = rInput.Columns.Count
UseHeaders = MsgBox(“Use Table Headers for your ” & R & “-row by ” & C & “-column table?”, _
vbYesNoCancel + vbQuestion, “DK’s Table Maker”)
If UseHeaders = vbCancel Then Exit Sub
sReturn = “.LT.table border=1 rules=all cellpadding=” & DQ & “5” & DQ & “.GT.”
If UseHeaders = vbYes Then
sReturn = sReturn & “.LT.tr.GT..LT.th bgcolor = #0055e5.GT. .LT./th.GT.”
For Each rCell In rInput.Rows(1).Cells
sReturn = sReturn & “.LT.th bgcolor = #0055e5 align=” & _
DQ & “center” & DQ & “.GT.” & “.LT.font face=” & _
DQ & “Arial” & DQ & “.GT.” & Chr$(rCell.Column + 64) & _
“.LT./font.GT..LT./th.GT.”
Next rCell
sReturn = sReturn & “.LT./tr.GT.” & vbNewLine
End If
For Each rRow In rInput.Rows
sReturn = sReturn & “.LT.tr.GT.”
If UseHeaders = vbYes Then
sReturn = sReturn & “.LT.th bgcolor = #0055e5 align=” & _
DQ & “center” & DQ & “.GT.” & “.LT.font face=” & _
DQ & “Arial” & DQ & “.GT.” & rRow.Row & “.LT./font.GT..LT./th.GT.”
End If
For Each rCell In rRow.Cells
CellContents = rCell.Text
If Len(CellContents) = 0 Then CellContents = “ ”
Select Case rCell.HorizontalAlignment
Case xlGeneral
TextAlign = “left”
If IsNumeric(rCell.Value) Then TextAlign = “right”
If IsError(rCell.Value) Then TextAlign = “center”
Case xlLeft
TextAlign = “left”
Case xlCenter
TextAlign = “center”
Case xlRight
TextAlign = “right”
Case xlJustify
TextAlign = “center”
End Select
FontFace = DQ & rCell.Font.Name & DQ
‘FontSize = rCell.Font.Size
‘If FontSize .LT. 12 Then FontSize = 12
TEMP = rCell.Font.Color
Red = Hex(TEMP And 255)
Green = Hex(TEMP 256 And 255)
Blue = Hex(TEMP 256 ^ 2 And 255)
If Len(Red) = 1 Then Red = “0” & Red
If Len(Green) = 1 Then Green = “0” & Green
If Len(Blue) = 1 Then Blue = “0” & Blue
FontColor = “#” & Red & Green & Blue
TEMP = rCell.Interior.Color
Red = Hex(TEMP And 255)
Green = Hex(TEMP 256 And 255)
Blue = Hex(TEMP 256 ^ 2 And 255)
If Len(Red) = 1 Then Red = “0” & Red
If Len(Green) = 1 Then Green = “0” & Green
If Len(Blue) = 1 Then Blue = “0” & Blue
BgColor = “#” & Red & Green & Blue
sReturn = sReturn & “.LT.td align=” & TextAlign & _
” bgcolor=” & BgColor & “.GT.”
sReturn = sReturn & “.LT.font face=” & FontFace & _
” color=” & FontColor & “.GT.”
With rCell.Font
If .Italic Then sReturn = sReturn & “.LT.i.GT.”
If .Bold Then sReturn = sReturn & “.LT.b.GT.”
If .Underline .LT..GT. xlNone Then sReturn = sReturn & “.LT.u.GT.”
If .Strikethrough Then sReturn = sReturn & “.LT.strike.GT.”
If .Subscript Then sReturn = sReturn & “.LT.sub.GT.”
If .Superscript Then sReturn = sReturn & “.LT.sup.GT.”
End With
sReturn = sReturn & CellContents
With rCell.Font ‘in reverse order
If .Superscript Then sReturn = sReturn & “.LT./sup.GT.”
If .Subscript Then sReturn = sReturn & “.LT./sub.GT.”
If .Strikethrough Then sReturn = sReturn & “.LT./strike.GT.”
If .Underline .LT..GT. xlNone Then sReturn = sReturn & “.LT./u.GT.”
If .Bold Then sReturn = sReturn & “.LT./b.GT.”
If .Italic Then sReturn = sReturn & “.LT./i.GT.”
End With
sReturn = sReturn & “.LT./font.GT..LT./td.GT.”
Next rCell
sReturn = sReturn & “.LT./tr.GT.” & vbNewLine
Next rRow
sReturn = sReturn & “.LT./table.GT.”
DataObj.SetText sReturn
DataObj.PutInClipboard
End Sub
In the above replace the ampersand-amp-semicolon with an ampersand, the .LT. with < (35 times) and the .GT. with > (34 times.) The macro accurately reproduced a very ugly selected range as this double-ugly table:
A | B | C | D | E | F | G | H | I | |
---|---|---|---|---|---|---|---|---|---|
1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
2 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 |
3 | 3 | #N/A | 9 | 12 | 18 | 21 | 24 | ||
4 | 4 | 8.00E+00 | 12 | 16 | 20 | 24 | 28.000 | 32 | 36 |
5 | The | quick | brown | fox | jumps | over | the | lazy | dog! |
6 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 |
That table uses every color in the default Excel palette, and the fonts use most of them. The fonts in Row 5, from left to right, are:
- Courier new
- Times New Roman
- Verdana
- Comic Sans MS
- Georgia
- Tahoma
- Trebuchet MS
- Arial Black
- Impact
There are assorted alignments and number formats sprinkled throughout. If the spreadsheet cell is empty, the macro puts a non-breaking space in the table as a placeholder. There is a considerable amount of bloat in the output, as it’s all done at the cell level. v3.0 will swap out the message-box interaction with a form that allows you to pick only what you are interested in. It’ll be out someday. Seeing how long it took MS to get Excel to edit at the character level, that’ll never be out.
Somethings I learned about WordPress: WordPress prefers the colors in #HEX format. When I used RBG, WordPress would wrap the RGB(r,g,b) in double quotes, and then not honor it! The fix was to use the HEX() function and a leading octothorpe. It turns out, WordPress wraps all the parameters, anyway. I don’t know how to capture the heading colors (I suspect it takes an API) so for now, whatever is the DDoE WordPress default (looks like a pale beige to me) is what you get here. (The sub’s code actually sets Windows Classic for the headers, but it gets overridden by CSS. Just as welldoesn’t really look like a window.) And I don’t think WordPress does subscript well when there’s no text to subordinate to. Cell C2 is specifed as superscript and E3 as subscript/strike-through. All I can say is that it’s clear on the spreadsheet.
…mrt:roll: