エクセルの表をHTMLのtableとして出力する
範囲選択したエクセルの表をHTMLのtableとして出力するVBAを作成してみます。
このVBAは以下のプログラムを組み合わせてできています。
- inputboxを使用した範囲選択
- テキストファイルの出力
- 選択した範囲の行数と列数の取得
参考記事
各プログラムの解説は個別の記事があるのでよければそちらを参照してみてください。

Excel VBA マウスのドラッグで任意の範囲を選択させる方法
VBAを使ってマウスで任意の範囲を選択させる方法VBAで処理させたいデータのセルの範囲を任意で選択させたいことがありませんか?ということで任意の範囲を選択させてみようと思います。サンプルコードSub selectRange() Dim ce...

Excel VBA テキストファイルを出力する方法 Openステートメント
VBAでテキストファイルを出力する方法エクセルを使用してテキストファイルを出力してみます。VBAでテキストファイルを出力する方法は2つこの記事で紹介するOpenステートメントとFileSystemObjectを使用した方法があります。どっち...
サンプルコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | Sub createTable() Dim selectRange As Range Dim rowStart As Integer Dim rowCount As Integer Dim columnStart As Integer Dim columnCount As Integer Dim outputHTML As String outputHTML = "C:\tmp\table.html" Open outputHTML For Output As #1 On Error Resume Next Set selectRange = Application.InputBox( "処理範囲をドラッグして選択してください" , "処理範囲の指定" , Type:=8) If selectRange Is Nothing Then Exit Sub rowStart = selectRange.row rowCount = selectRange.Rows.Count columnStart = selectRange.Column columnCount = selectRange.Columns.Count Print #1, "<table>" Dim i, j As Integer For i = rowStart To rowStart + rowCount - 1 Print #1, "<tr>" '先頭行(1ループ目)は、見出し行のため<th>を出力 If rowStart = i Then For j = columnStart To columnStart + columnCount - 1 Print #1, "<th>" & Cells(i, j) & "</th>" Next j Else For j = columnStart To columnStart + columnCount - 1 Print #1, "<td>" & Cells(i, j) & "</td>" Next j End If Print #1, "</tr>" Next i Print #1, "</table>" Close #1 End Sub |
コメント