MS Office Programming tricks Tips Tricks Useful

How to Convert an Excel Table to HTML Table

Lets consider we have some data in an Excel table and we want to display that in a webpage. If the table is a small one, then it is OK. We can manually write the HTML code for it. But if the Excel table is bit lengthier then what to do, how to convert a big table to HTML. Yes, today I am going to help you for the same purpose. You can easily convert an Excel table to its HTML equivalent with a tiny script. Just go through the following steps.
  • Open the Excel file containing the table
  • Press alt+f11 to open the Visual Basic Editor
  • In the editor you can see the sheet “Sheet1” in the left side bar as shown in the figure
Visual Basic Editor
  • Double click on Sheet1 to open the editor on right side.
  • Now copy and paste the following code in the editor. No need to change anything in the code.
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub ExceltoHTML()
Dim lastRow As Integer
Dim lastColumn As Integer
Dim i As Integer
Dim j As Integer
Dim inName As String
Dim confirm As VbMsgBoxResult

lastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
lastColumn = Sheet1.Cells(1, Columns.Count).End(xlToLeft).Column
confirm = MsgBox("Is the first row is the table header ?", vbYesNoCancel, "Confirm")
If (confirm = vbCancel) Then
GoTo theend
End If
If (vbYes = MsgBox("After pressing YES, you will not move your mouse or click anywhere untill the entire table is completely exported. Do you want to export ?", vbYesNoCancel, "Message")) Then

If (vbYes = confirm) Then
Shell "Notepad.exe", vbNormalFocus
Application.Wait (Now + TimeValue("0:00:02"))
SendKeys "<table border='1' cellpadding='3' cellspacing='1' style='font-family:Georgia'>{ENTER}{TAB}<tr>{ENTER}", True
Sleep (20)
For i = 1 To lastColumn
SendKeys "{TAB}{TAB}<th>", True
Sleep (20)
inName = Split(Cells(, i).Address, "$")(1)
Sleep (20)
SendKeys Sheet1.Range(inName + "1").Value, True
Sleep (20)
SendKeys "</th>{ENTER}", True
Sleep (20)
Next
Else
Shell "Notepad.exe", vbNormalFocus
Application.Wait (Now + TimeValue("0:00:02"))
SendKeys "<table border='1' cellpadding='3' cellspacing='1' style='font-family:Georgia'>{ENTER}{TAB}<tr>{ENTER}", True
Sleep (20)
For i = 1 To lastColumn
SendKeys "{TAB}{TAB}<td>", True
Sleep (20)
inName = Split(Cells(, i).Address, "$")(1)
Sleep (20)
SendKeys Sheet1.Range(inName + "1").Value, True
Sleep (20)
SendKeys "</td>{ENTER}", True
Sleep (20)
Next
End If

SendKeys "{TAB}</tr>{ENTER}", True

For i = 2 To lastRow
SendKeys "{TAB}<tr>{ENTER}", True
Sleep (20)
For j = 1 To lastColumn
SendKeys "{TAB}{TAB}<td>", True
Sleep (20)
inName = Split(Cells(, j).Address, "$")(1)
SendKeys Sheet1.Range(inName + Mid(Str(i), 2, 4)).Value, True
Sleep (20)
SendKeys "</td>{ENTER}", True
Sleep (20)
Next
SendKeys "{TAB}</tr>{ENTER}", True
Sleep (20)
Next
SendKeys "</table>", True
Sleep (20)
End If
theend:
End Sub
  • After you paste the code just close the visual basic window. No need to save.
  • Now go to the Excel table and press alt+f8 to open the Macro window.
  • In the Macro window you can see a Macro having name “ExceltoHTML“. Select the Macro and click on Run.
  • Now you are done. The Excel table will be automatically exported in a Notepad as an HTML table.
  • You have to strictly obey only one rule. While the table is being exported you don’t have to move your mouse or click anywhere. Just sit calmly and wait for the file to be completely exported. After the table is exported you can do whatever you want.
  • The above rule is mentioned because the table is exported using VB Sendkeys. So if you will interrupt in between it will not work.

If anybody facing any problem then drop a comment.

You may also like to see the reverse process i.e inserting an HTML table into the Excel.

1 thought on “How to Convert an Excel Table to HTML Table”

  1. after successfully writing several rows it gives a error “RUN-TIME ERROR 5”-“INVALID PROCEDURE CALL OR ARGUMENT

Leave a Reply

Your email address will not be published. Required fields are marked *