Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

automating simple HTML formatting from excel

Latest post Tue, Mar 18 2008 6:03 PM by Nick Hodge. 3 replies.
  • Mon, Mar 17 2008 6:39 PM

    • veradusit
    • Top 75 Contributor
    • Joined on Mon, Mar 17 2008
    • Posts 5
    • Points 120

    automating simple HTML formatting from excel

     I hope this request is not to mundane for this forum.  I have an excel spreadsheet of data that I would like to format with some basic html code, and then upload that sheet to google docs.

    Here is a sample of the spreadsheet:

    http://spreadsheets.google.com/pub?key=pAJGKGh0KC96soiK3FBsILQ

     The code in question is on worksheet "PlacemarkData" Cell J11

    <p><b>Built In:</b> 2000</p>
    <p><b>Address</b>
      <p>141 N Davidson Rd</p>
      <p>Vail,&nbsp;AZ&nbsp;85641 </p>
      <p> <b>Market Value: </b>$513000.00</p>
      <p><b>Range:</b>(Low-$454000.00-High-$572000.00)</p>
      <p><b>Sale Date:</b> 4/28/2006</p>
      <p><b>Sale Amount:</b>$295000.00</p>

     

    This bit of html code (currently) is being produced with a "mail-merge" within word (No laughing please) and then the resulting code is pasted into the google spreadsheet in the appropriate cell.  

     

    The data that populates the mail merge is in excel and I'm sure there is a simple VBA macro that could do this easy enough.  I tried the concatenate function and the arguments were too long for the entire html code to be concatenated.   

    columns on the spreadsheet would be as follows

    YrBuilt,  Address, City, State, ZipCode, MktVal, MktLow, MktHigh, SaleDate,  SaleAmt

     

    and may have 100-150 rows of data.

     

    Thanks in advance for being patient with a realtive VBA virgin. 

    • Post Points: 52
  • Tue, Mar 18 2008 4:46 AM In reply to

    • Nick Hodge
    • Top 10 Contributor
    • Joined on Sun, Dec 23 2007
    • Southampton
    • Posts 336
    • Points 5,771
    • MVP

    Re: automating simple HTML formatting from excel

    This is s little difficult to invisage between the description and google docs. Can you attach a file to this thread and I'll take a look, or mail me a sample of the data in Excel and I'll get it done and post back to everyone's benefit. (Let me know what version of excel too)

    PS: I do have a day job so maybe later this evening or tomorrow?

    Regards
    Nick Hodge
    Microsoft MVP, Excel
    Southampton, UK

    • Post Points: 5
  • Tue, Mar 18 2008 4:53 AM In reply to

    Betreft: automating simple HTML formatting from excel

    Something like this should do the trick, maybe it gets you started:

     Option Explicit

    Sub Test()
        Dim sCode As String
        Dim oCell As Range
        For Each oCell In Intersect(ActiveSheet.UsedRange, ActiveSheet.Columns(1))
            'Skip row 1
            If oCell.Row > 1 Then
                sCode = "<p><b>Built In:</b> " & oCell.Value & "</p>"
                sCode = sCode & "<p><b>Address</b>"
                sCode = sCode & "<p>" & oCell.Offset(, 1).Value & "</p>"
                sCode = sCode & "<p>" & oCell.Offset(, 2).Value & "</p>"
                sCode = sCode & "<p> <b>Market Value: </b>" & oCell.Offset(, 3).Value & "</p>"
                sCode = sCode & "<p><b>Range:</b>" & oCell.Offset(, 4).Value & "</p>"
                sCode = sCode & "<p><b>Sale Date:</b>" & oCell.Offset(, 5).Value & "</p>"
                sCode = sCode & "<p><b>Sale Amount:</b>" & oCell.Offset(, 6).Value & "</p>"
                'Now do something with sCode
            End If
        Next
    End Sub

     

    Regards, Jan Karel Pieterse www.jkp-ads.com
    • Post Points: 5
  • Tue, Mar 18 2008 6:03 PM In reply to

    • Nick Hodge
    • Top 10 Contributor
    • Joined on Sun, Dec 23 2007
    • Southampton
    • Posts 336
    • Points 5,771
    • MVP

    Re: automating simple HTML formatting from excel

    Will here is the code for your sample. This finds the extent of the data, builds the data from their respective cells and then builds the HTML version and finally places that in the tenth column of the row that t has built the data for.

    You only need make sure the data sheet is the active one before running the code.

    Sub BuildHTML()
    
    Dim lLastRow As Long, wks As Worksheet, rng As Range
    Dim APN As String, Address As String, MarketValue As String
    Dim LoanAmount As String, RangeLow As String, RangeHigh As String, Owner As String
    Dim LandOwner As String, Legal As String, AddInfo As String
    Dim finalHTML As String
    
    Set wks = ActiveSheet
    lLastRow = wks.Range("A65536").End(xlUp).Row
    
    For Each rng In wks.Range(Cells(2, 1), Cells(lLastRow, 1))
        With rng
            APN = .Offset(0, 0).Value
            Address = .Offset(0, 1).Value
            MarketValue = Format(.Offset(0, 3).Value, "$#,##0")
            LoanAmount = Format(.Offset(0, 2).Value, "$#,##0")
            RangeLow = Format(.Offset(0, 5).Value, "$#,##0.00")
            RangeHigh = Format(.Offset(0, 4).Value, "$#,##0.00")
            Owner = .Offset(0, 6).Value
            LandOwner = .Offset(0, 7).Value
            Legal = .Offset(0, 9).Value
            AddInfo = .Offset(0, 8).Value
        End With
        finalHTML = "<p><b>APN:</b> " & APN & "</p>" & Chr(10) & _
                    "<p><b>Address</b>" & Chr(10) & "<p>" & Address & "</p>" & Chr(10) & _
                    "<p><b>Market Value:</b> " & MarketValue & "</p>" & Chr(10) & _
                    "<p><b>Loan Amount:</b> " & LoanAmount & "</p>" & Chr(10) & _
                    "<p><b>Range:</b> (Low - " & RangeLow & " - High - " & RangeHigh & ")</p>" & Chr(10) & _
                    "<p><b>Owners Name:</b> " & Owner & "</p>" & Chr(10) & _
                    "<p><b>Land Owner's Name:</b> " & LandOwner & "</p>" & Chr(10) & _
                    "<p><b>Legal Info:</b> " & Legal & "</p>" & Chr(10) & _
                    "<p><b>Additional Info</b>" & Chr(10) & _
                    "<p>" & AddInfo & "</p>"
        rng.Offset(0, 10).Value = finalHTML
    Next rng
    End Sub
    

    (Sample file returned to Will)

    Regards
    Nick Hodge
    Microsoft MVP, Excel
    Southampton, UK

    Filed under: ,
    • Post Points: 5
Page 1 of 1 (4 items) | RSS
Copyright Excel User Group and the relevant contributors, 2008. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.