Guest User

Formatting HTML data in excel

a guest
May 13th, 2013
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.28 KB | None | 0 0
  1. Sub main()
  2.     Dim Ie As Object
  3.     On Error Resume Next
  4.    
  5. '    Worksheets.Add(After:=Worksheets(1)).Name = "Sheet1"
  6.     Workbooks("Defects.xls").Sheets("Query1").Select
  7.     Columns("A:AB").Select
  8.     Selection.NumberFormat = "General"
  9.     Columns("J:N").Select
  10.     Selection.NumberFormat = "m/d/yyyy"
  11.     Set Ie = CreateObject("InternetExplorer.Application")
  12.     For i = 2 To Sheets("Query1").UsedRange.Rows.Count
  13.         With Ie
  14.             .Visible = False
  15.             .Navigate "about:blank"
  16.             .document.body.InnerHTML = Sheets("Query1").Cells(i, 6).Value
  17.             .document.body.createtextrange.execCommand "Copy"
  18.             ActiveSheet.Paste Destination:=Sheets("Query1").Range("AD1")
  19.         End With
  20.         LastRow = Sheets("Query1").Range("AD65536").End(xlUp).Row
  21.         Call ConcatenateRichText(Sheets("Query1").Range("F" & i), Sheets("Query1").Range("AD1:AD" & LastRow))
  22.         Trim (Sheets("Query1").Range("F" & i))
  23.     Workbooks("Defects.xls").Sheets("Query1").Select
  24.     Columns("AD:AD").Select
  25.     Selection.ClearContents
  26.     Next
  27.     Ie.Quit
  28.     MsgBox "Finished"
  29. End Sub
  30. Sub ConcatenateRichText(Target As Range, Source As Range)
  31.     Dim Cell As Range
  32.     Dim i As Long
  33.     Dim c As Long
  34.     i = 1
  35.     With Target
  36.         .Clear
  37.         For Each Cell In Source
  38.             .Value = .Value & vbLf & Cell.Value
  39.         Next Cell
  40.         .Value = Trim(.Value)
  41.     End With
  42.     For Each Cell In Source
  43.         For c = 1 To Len(Cell.Value)
  44.             With Target.Characters(i, 1).Font
  45.                 .Name = Cell.Characters(c, 1).Font.Name
  46.                 .FontStyle = Cell.Characters(c, 1).Font.FontStyle
  47.                 .Size = Cell.Characters(c, 1).Font.Size
  48.                 .Strikethrough = Cell.Characters(c, 1).Font.Strikethrough
  49.                 .Superscript = Cell.Characters(c, 1).Font.Superscript
  50.                 .Subscript = Cell.Characters(c, 1).Font.Subscript
  51.                 .OutlineFont = Cell.Characters(c, 1).Font.OutlineFont
  52.                 .Shadow = Cell.Characters(c, 1).Font.Shadow
  53.                 .Underline = Cell.Characters(c, 1).Font.Underline
  54.                 .ColorIndex = Cell.Characters(c, 1).Font.ColorIndex
  55.             End With
  56.             i = i + 1
  57.         Next c
  58.         i = i + 1
  59.     Next Cell
  60. End Sub
Advertisement
Add Comment
Please, Sign In to add comment