Advertisement
fslasht

MakeBootStrapHtmlTable VBA

Aug 8th, 2013
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' Excel表の選択範囲のセルをもとにTwitterBootstrap2.xのテーブル(HTML)を生成する
  2. ' 2013.08.09 Fukaya
  3.  
  4. ' ※生成されたHTMLはクリップボードにコピーされます。
  5. ' ※1行目はヘッダ行になります
  6. ' ※メニューの「ツール」→「参照設定」で「Microsoft Forms 2.0 Object Library」を選択しておく必要があります。(クリップボード操作のため)
  7.  
  8. Option Explicit
  9. Dim htmlOut As String ' 生成されるHTML
  10.  
  11. Sub MakeBootStrapHtmlTable()
  12.     ' 選択範囲
  13.    Dim x0 As Integer
  14.     Dim y0 As Integer
  15.     Dim w As Integer
  16.     Dim h As Integer
  17.  
  18.     ' 変数
  19.    Dim x As Integer
  20.     Dim y As Integer
  21.     Dim rowHtml As String
  22.  
  23.  
  24.     ' 選択範囲取得
  25.    x0 = Selection.Cells(1).Column
  26.     y0 = Selection.Cells(1).Row
  27.     w = Selection.Cells(Selection.Count).Column - x0 + 1
  28.     h = Selection.Cells(Selection.Count).Row - y0 + 1
  29.    
  30.     Debug.Print "選択範囲:" & x0 & "," & y0 & "," & w & "," & h
  31.    
  32.     If w < 1 Or h < 2 Then
  33.         MsgBox "選択範囲が狭すぎます。" & vbCrLf & "1桁2行以上を選択する必要があります。"
  34.         Exit Sub
  35.     End If
  36.    
  37.        
  38.     ' 生成されるHTML初期化
  39.    ClearHtml
  40.    
  41.     ' 処理開始
  42.    ' テーブル
  43.    AddHtml 0, "<table class='table table-striped table-bordered table-condensed'>"
  44.    
  45.     ' テーブルヘッダ
  46.    AddHtml 1, "<thead>"
  47.     rowHtml = "<tr>"
  48.  
  49.     For x = x0 To x0 + w - 1
  50.         rowHtml = rowHtml & "<th>" & MakeHtml(Cells(y0, x)) & "</th>"
  51.     Next
  52.     rowHtml = rowHtml & "</tr>"
  53.     AddHtml 2, rowHtml
  54.     AddHtml 1, "<thead>"
  55.    
  56.     ' データブロック
  57.    AddHtml 1, "<tbody>"
  58.    
  59.  
  60.     For y = y0 + 1 To y0 + h - 1
  61.    
  62.         ' データ行
  63.        rowHtml = "<tr>"
  64.         For x = x0 To x0 + w - 1
  65.             rowHtml = rowHtml & "<td>" & MakeHtml(Cells(y, x)) & "</td>"
  66.         Next
  67.         rowHtml = rowHtml & "</tr>"
  68.         AddHtml 2, rowHtml
  69.     Next
  70.     AddHtml 1, "<tbody>"
  71.    
  72.     ' /テーブル
  73.    AddHtml 0, "</table>"
  74.    
  75.     ' 完了
  76.    Debug.Print htmlOut
  77.    
  78. '   'A) InputBoxで結果を表示する場合(文字列長制限あり)
  79. '    InputBox "コピーしてお使いください", "テーブル用HTMLを生成しました。", htmlOut
  80.      
  81.     'B) クリップボードにコピーする場合(参照設定が必要です)
  82.    CopyToClipboard htmlOut
  83.     MsgBox "テーブル用HTMLを生成しました。" + vbCrLf + "クリップボードにコピーしました。"
  84.    
  85. End Sub
  86.  
  87. Sub ClearHtml()
  88.     htmlOut = ""
  89. End Sub
  90.  
  91.  
  92. Sub AddHtml(tabs As Integer, str As String)
  93.     Dim i As Integer
  94.     For i = 0 To tabs - 1
  95.         htmlOut = htmlOut & vbTab
  96.     Next
  97.     htmlOut = htmlOut & str & vbCrLf
  98. End Sub
  99.  
  100. Function MakeHtml(str As String) As String
  101.     str = Replace(str, vbLf, "<br/>")
  102.     MakeHtml = str
  103. End Function
  104.  
  105. Sub CopyToClipboard(str As String)
  106.     Dim MyDataObject As DataObject
  107.     Set MyDataObject = New DataObject
  108.     MyDataObject.SetText str
  109.     MyDataObject.PutInClipboard
  110. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement