Solved

How to convert html to excel using vba

Posted on 2011-09-26
8
1,272 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

708 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

12 Experts available now in Live!

Get 1:1 Help Now