2017-10-19 14 views
0

誰でもこのコードを実行可能にする手助けはできますか?VBA ExcelコンテンツをHTMLに作成する(ExcelからHTMLファイルを作成する)

ここで考えられるのは、異なる行の情報を含む設定行を収集し、この情報をすべてHTMLファイルに挿入することです。

あなたに私は非常に感謝するだろうコードのいくつかの特典:

1)は、8Pの仕事作ります。 2)通常の保存ウィンドウのようにファイルを保存する場所をユーザーが選択できるようにする(可能でない場合、少なくとも割り当てられたフォルダ内のファイルの名前を選択させる)。 3)コードが行の空でない行をすべてキャプチャしていることを確認します。

注目のコミュニティに多くの感謝!

何が起こったのかは怒鳴ります。

Sub CreateHTML() 
'Define your variables. 
    Dim iRow As Long 
    Dim iStage As Integer 
    Dim iCounter As Integer 
    Dim iPage As Integer 

    'Create an .htm file in the assigned directory. 
    Dim sFile As String 
    sFile = "J:\GEROC1\Avaliação RO\4) CICLOS\ArquivosExportados" & "\test.html" 
    Close 

    'Open up the temp HTML file and format the header. 
    Open sFile For Output As #1 
    Print #1, "<html>" 
    Print #1, "<head>" 
    Print #1, "<style type=""text/css"">" 
    Print #1, "table {font-size: 16px;font-family: Optimum, Helvetica, sans-serif;Border -collapse: collapse}" 
    Print #1, "tr {border-bottom: 1px solid #A9A9A9;}" 
    Print #1, "td {padding: 4px; margin: 3px; padding-left: 20px; width: 75%; text-align: justify;}" 
    Print #1, "th { background-color: #A9A9A9; color: #FFF; font-weight: bold; font-size: 28px; text-align: center;}" 
    Print #1, "</style>" 
    Print #1, "</head>" 
    Print #1, "<body>" 
    Print #1, "<table class=""table""><thead><tr class=""firstrow""><th colspan=""2"">Ficha de Risco</th></tr></thead><tbody>" 

    'Start on the 2nd row to avoid the header. 
    iRow = 2 

    'Translate the first column of the table into the first level of the hierarchy. 
    Do While WorksheetFunction.CountA(Rows(iRow)) > 0 
     If Not IsEmpty(Cells(iRow, 23)) Then 
     For iCounter = 1 To iStage 
      'Print #1, "</ul>" 
      iStage = iStage - 1 
     Next iCounter 
     Print #1, Cells(iRow, 1).Value 
     iPage = iPage + 1 
     If iStage < 1 Then 
      iStage = iStage + 1 
     End If 
     End If 
    Loop 

    'Add ending HTML tags 
    Print #1, "</body>" 
    Print #1, "</html>" 
    Close 
End Sub 
+0

ループにはHTMLを追加していません。 'tr'と' td'テーブルに内容を追加する必要があります。 – Patrick

+0

Ops私は言いたいことを忘れていました...彼が印刷しているセルはすでにtrとtdステートメントを持っています... – PunchTheNewbie

答えて

0

以下のコードは、Excelファイルのテーブルからhtmlファイルを作成します。テーブル全体を書いて、行と列の数を検索して表示します。

テーブル自体のhtmlセクションを編集してください。私の例では、行と列を転置して1つずつ印刷したいと考えました。

また、表のメインヘッダーにタイトルを付けるかどうかを調整してください。 colspanを調整することを忘れないでください。

この例では、テーブルをhtmlファイルに書き込むように調整することができます。

最後に、ファイルを保存する場所を尋ねる少し余分なスペースを入れます。

Sub CreateHTML() 
    'Define your variables. 
    Dim iRow As Long 
    Dim tRow As Long 
    Dim iStage As Integer 
    Dim iCounter As Integer 
    Dim iPage As Integer 
    Dim lastCol As Integer 
    Dim lastRow As Integer 

    'Find the last Column Number 
    With ActiveSheet 
     lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column 
    End With 

    'Find the last Column Row 
    With ActiveSheet 
     lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 
    End With 

    'Create an .html file in the assigned directory. 
    Dim sFile As Variant 

    sFile = Application.GetSaveAsFilename(fileFilter:="HTML Files (*.html), *.htm") 

    If fileName <> False Then 
    SaveWorkbook = fileName 
    End If 

    'Open up the temp HTML file and format the header. 
    Open sFile For Output As #1 
    Print #1, "<html>" 
    Print #1, "<head>" 
    Print #1, "<style type=""text/css"">" 
    Print #1, "table {font-size: 16px;font-family: Optimum, Helvetica, sans-serif; border-collapse: collapse}" 
    Print #1, "tr {border-bottom: thin solid #A9A9A9;}" 
    Print #1, "td {padding: 4px; margin: 3px; padding-left: 20px; width: 75%; text-align: justify;}" 
    Print #1, "th { background-color: #A9A9A9; color: #FFF; font-weight: bold; font-size: 28px; text-align: center;}" 
    Print #1, "td:first-child { font-weight: bold; width: 25%;}" 
    Print #1, "</style>" 
    Print #1, "</head>" 
    Print #1, "<body>" 
    Print #1, "<table class=""table""><thead><tr class=""firstrow""><th colspan=""2"">INSERT TABLE MAIN HEADER HERE - WATCH OUT FOR TABLE COLSPAN</th></tr></thead><tbody>" 

    'Translate the first column of the table into the first level of the hierarchy. 
    tRow = 1 

    'Start on the 2nd row to avoid the header. - iRow=2/tRow is the table header 
    For iRow = 2 To lastRow 


    For iCounter = 1 To lastCol 

    'EDIT HERE TO CHANGE IT TO YOUR LINKING 
    Print #1, "<tr><td>" 
    Print #1, Cells(tRow, iCounter).Value 
    Print #1, "</td><td>" 
    Print #1, Cells(iRow, iCounter).Value 
    Print #1, "</td></tr>" 

    Next iCounter 
    Next iRow 

    'Add ending HTML tags 
    Print #1, "</body>" 
    Print #1, "</html>" 
    Close 
    End Sub 
関連する問題