Solved

How to convert html to excel using vba

Posted on 2011-09-26
8
1,353 Views
Last Modified: 2012-05-12
How do I convert html to excel using vba?

Basically I have a string that contains html, and I would like to copy and paste it to another sheet as excel (in columns).

Here is an example html that i would like to convert.

<html>
      <head>
            <title>UtiliQuote - Use Your Power</title>
      </head>
      <body bgcolor="C0C0C0" leftmargin="0" topmargin="0">
            <table width="100%" border="0">
                        <tr>
                              <td align="center">4315592015</td>
                              <td align="center">account_OK</td>            
                              <td align="center">usage_KO</td>
                        </tr>
                        <tr>
                              <td align="center">NameOfClien</td>
                              <td align="center">Address</td>
                              <td align="center"></td>
                              <td align="center">IL</td>
                              <td align="center">60000</td>
                              <td align="center">1234567890</td>
                              <td align="center">12</td>
                              
                              <td align="center"></td>
                              <td align="center"></td>
                              <td align="center"></td>
                              <td align="center"></td>
                        </tr>
                        <tr>
                              <td align="center">0</td>
                              <td align="center">account_KO</td>            
                              <td align="center">usage_KO</td>
                        </tr>
                        <tr>
                              <td align="center">0</td>
                              <td align="center">account_KO</td>            
                              <td align="center">usage_KO</td>
                        </tr>
                        <tr>
                              <td align="center">0</td>
                              <td align="center">account_KO</td>            
                              <td align="center">usage_KO</td>
                        </tr>
                        <tr>
                              <td align="center">0</td>
                              <td align="center">account_KO</td>            
                              <td align="center">usage_KO</td>
                        </tr>
                        <tr>
                              <td align="center">0</td>
                              <td align="center">account_KO</td>            
                              <td align="center">usage_KO</td>
                        </tr>
                        <tr>
                              <td align="center">0</td>
                              <td align="center">account_KO</td>            
                              <td align="center">usage_KO</td>
                        </tr>
                        <tr>
                              <td align="center">0</td>
                              <td align="center">account_KO</td>            
                              <td align="center">usage_KO</td>
                        </tr>
                        <tr>
                              <td align="center">0</td>
                              <td align="center">account_KO</td>            
                              <td align="center">usage_KO</td>
                        </tr>
                        <tr>
                              <td align="center">0</td>
                              <td align="center">account_KO</td>            
                              <td align="center">usage_KO</td>
                        </tr>
            </table>
      </body>
</html>
0
Comment
Question by:consoleboy
  • 4
  • 3
8 Comments
 
LVL 7

Expert Comment

by:m4trix
ID: 36617359
It's doable, but not super easy.  What is the source of the html? If it is from online, is there a reason you can't or don't want to import it as a web query?
0
 

Author Comment

by:consoleboy
ID: 36620739
the web service is very flaky, so using web query is out of the question because it hangs excel 5 out of 10 times.

So I build a .net excel extension that returns the html as a string.
0
 
LVL 7

Expert Comment

by:m4trix
ID: 36622383
strange, I can't say I've ever found Excel's web query to be "flaky" myself.  Next question then, is the HTML always going to be in that format? If the format of the data is always similar then it's much easier to create something to parse it because it doesn't need to be generic
0
 

Author Comment

by:consoleboy
ID: 36630530
i meant to say that sometimes the web service itself is flaky.
It does not respond or returns error messages (even when sending the same request).

So to get around that we put together an excel c# extension that retrurns a string with the html.

The information returned from the html varies (number of rows), but the n umberare always the same.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 26

Expert Comment

by:redmondb
ID: 36631610
consoleboy,

Couple of quick and (very) dirty possibilities...

(1) Output the string to a new html file. Open the html file in Excel. Copy and paste the data.

(2) Copy the string to the clipboard. Paste it to the required location. The "dirty" part of this is that Excel doesn't parse all the formatting (specifically the background colour).

Regards,
Brian.
0
 
LVL 7

Accepted Solution

by:
m4trix earned 500 total points
ID: 36642282
here's a really simple solution. It does not consider formatting at all, it simply takes an HTML table and spits it out into an excel sheet, row for row, column for column.

Sub readHTML(html)
    Dim xmlObj As New MSXML2.DOMDocument
    Dim tRow As MSXML2.IXMLDOMNode
    Dim tCell As MSXML2.IXMLDOMNode
    Dim row As Integer, col As Integer
    
    row = 1

    'Load the HTML into an XML object
    xmlObj.LoadXML html
    
    'Loop through all childnodes of the "table" tag (should all be table rows [<tr>])
    For Each tRow In xmlObj.getElementsByTagName("table").Item(0).ChildNodes
        If tRow.nodeName = "tr" Then
            col = 1
            ' Loop through each cell in the row, and output it to excel
            For Each tCell In tRow.ChildNodes
                If tCell.nodeName = "td" Or tCell.nodeName = "th" Then
                    Cells(row, col) = tCell.Text
                    col = col + 1
                End If
            Next tCell
            row = row + 1
        End If
    Next tRow
    
    Set tCell = Nothing
    Set tRow = Nothing
    Set xmlObj = Nothing
End Sub

Open in new window


You can see an example in the attached file. Node that the first sub ("testingSub") is just there so I could read the HTML into a single string to mimic what you would likely have. The first sheet shows the output when I run the macro with your sample html data

 Book1.xlsm
0
 
LVL 7

Expert Comment

by:m4trix
ID: 36642839
PS, you may need to add the "Microsoft XML" reference - I used v6.0, but you can select whatever the highest version you have
0
 

Author Closing Comment

by:consoleboy
ID: 36711387
Wonderful! Works beautifully.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now