2017-10-27 15 views
0

テーブルをhtml形式で印刷することは、私の目的にはほぼ完全なコードがあります。htmlファイル内のテキストをExcelのVBAに置き換える

1)通貨形式の番号(列17)で満たされている一つの列があります:

私は二つのことを欠けています。私がhtmlに書き込むと、その通貨フォーマットが失われてプレーンテキストになります。その特定の列のセルの印刷内容を、通貨形式でカンマを追加したテキスト形式に変更するコードを追加したいと思います。

2)物のリストが表示され、「;」で区切られたセルがいくつかあります。または"。私は、この文字の後に改行を入れて、次の行が ";
"と ";と
"に変更されるようにしたいと思います。 ";"で終わる他のhtml要素があるので、ここにキャッチがあります。その交換は、私がテーブルの内容を書いているセクションでのみでなければなりません。さもなければ、CSS属性の要素を変更するでしょう。

以下のコードを送信します。私はすべてのあなたの助けのために事前に感謝します。

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 
    Dim repLine As Variant 'the array of lines you will WRITE 
    Dim ln As Variant 
    Dim l As Long 

    '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>" 

    '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 

    Print #1, "<table class=""table""><thead><tr class=""firstrow""><th colspan=""2"">Ficha de Risco</th></tr></thead><tbody>" 

     For iCounter = 1 To lastCol 

    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>" 


    'after this comment the code is awfully wrong - please help! 
    'Replace ; with ; <br> and ; and with ; and <br> - insert a breaking point after semicollon 

    '# Read entire file into an array & close it 
    repLine = Split(sFile.ReadAll, vbNewLine) 

    '# iterate the array and do the replacement line by line 
    For Each ln In repLine 
    ln = IIf(InStr(1, ln, ";", vbTextCompare) > 0, Replace(ln, ";", "<br>"), ln) 
    repLine(l) = ln 
    l = l + 1 
    Next 

    '# Write to the array items to the file 
    sFile.Write Join(repLine, vbNewLine) 

    Close 


End Sub 
+1

'プリント#1、置換(セル(tRow、iCounter)。値、"; "、
") ' –

答えて

0

フォーマット機能を試しましたか?あなたは、カラム17を選び出すと、次のように書式を適用するには、いくつかの追加のコードを追加する必要があるかもしれませんが、これはあなたのために働くかもしれない:

Format("1267.5", "Currency") 

結果:「$ 1,267.50」を

0

@Timウィリアムズは右側に私を投げましたパス。

#2の要求のための最終的なコードは切り替えることによって達成される:

Print #1, Cells(iRow, iCounter).Value 

で:

Print #1, Replace(Replace(Cells(iRow, iCounter).Value, "; and", "<br>"), ";", "<br>") 

Aは別の内側に代わる代わる...

コードの残りの部分をうんざりされていたのは解消できない。

関連する問題