Excel tabel naar HTML overzetten

Je hebt een Excel tabel en wil die omzetten naar een HTML tabel. Selecteer een cel in de tabel. In de afbeelding is dat cel C6. Vervolgens onderstaande code in een module zetten. En dan de Sub:
Test_Tabel_Naar_HTML uitvoeren. Als de Excel tabel is overgezet wordt het resultaat naar het bestand C:\temp\textfile.html weg geschreven. Dubbelklikken op dat bestand en je ziet het resultaat in de browser. Zoiets.

Als eerste:

  1. Kopieer de onderstaande code middels Ctrl + C
  2. Druk op de toetscombinatie ALT + F11 om de Visual Basic Editor te openen
  3. Druk op de toetscombinatie ALT + N om het menu Invoegen te openen
  4. Druk op M om een standaard module in te voegen
  5. Daar waar de cursor knippert voeg je de code in middels Ctrl + V
  6. Druk op de toetscombinatie ALT + Q om de Editor af te sluiten en terug te keren naar Excel
  7. En dan de Sub: Test_Tabel_Naar_HTML uitvoeren.
Option Explicit

'VBA - Excel tabel naar HTML
'Eenvoudige code om een Excel tabel naar een HTML tabel te converteren.
'Om tabelrijen te markeren ga je er met de muis overheen.

Sub Test_Tabel_Naar_HTML()
    'Selecteer één cel in je tabel zodat Excel weet
    'Welke data naar de HTML tabel moet worden omgezet
    'Tevens wordt het eindresultaat geprint naar:
    'Direct; Venster / Immediate; Window
    Debug.Print Tabel_Naar_HTML(ActiveCell.CurrentRegion)
End Sub

Function Tabel_Naar_HTML(ByVal rng As Excel.Range)

    Dim strHTML_Code As String
    'Begin van de HTML code die altijd hetzelfde is
    strHTML_Code = "<!DOCTYPE html><html><head><style>table{border-collapse:collapse;width:100%}td,th{padding:8px;text-align:left;border-bottom:1px solid #DDD}tr:hover{background-color:#D6EEEE}</style></head><body><h2>Hoverable Table</h2><p>Move the mouse over the table rows to see the effect.</p><table>"
    
    'Door de rijen navigeren.
    Dim rngRowLoop As Excel.Range
    For Each rngRowLoop In rng.Rows
        strHTML_Code = strHTML_Code & "<tr>"
    
        Dim rngCellLoop As Excel.Range
        For Each rngCellLoop In rngRowLoop.Cells
        
            strHTML_Code = strHTML_Code & "<td style='border: 1px solid lightgrey;'>" & rngCellLoop.Value2 & "</td>"
        
        Next rngCellLoop
    
        strHTML_Code = strHTML_Code & "</tr>"
    Next
    
    'HTML afsluiten met "</table></body></html>"
    strHTML_Code = strHTML_Code & "</table></body></html>"
    
    Tabel_Naar_HTML = strHTML_Code
    
    'Naar bestand schrijven.
    Call Schrijf_Naar_Bestand(strHTML_Code)

End Function


Function Schrijf_Naar_Bestand(strTableData As String) As Boolean
    'Bestand textfile.html wordt automatisch aangemaakt
    Const LogFileName As String = "C:\temp\textfile.html"
    Dim FileNum As Integer
    
    'Volgende bestandsnummer
    FileNum = FreeFile
    
    'Maakt bestand aan indien niet aanwezig
    Open LogFileName For Append As #FileNum
    
    'Schrijft informatie weg aan het einde van het bestand
    Print #FileNum, strTableData
    
    'Sluit het bestand
    Close #FileNum
    
    'Gelukt
    Schrijf_Naar_Bestand = True
End Function

Leave a Reply

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