+ Reply to Thread
Results 1 to 6 of 6

Macro to select cells and save as html

Hybrid View

  1. #1
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Macro to select cells and save as html

    Hello itmanusa,

    Welcome to the Forum and Merry Christmas!

    This macro will create a HTML file for the range in column "A" and name the file using the contents of cell "A1".

    'Written: December 24, 2010
    'Author:  Leith Ross
    'Summary: Save the cells in column "A" staring with "A1" as an HTML file. The file is
    '         saved in the currrent directory and named using the contents of cell "A1".
    
    Sub SaveRangeAsHTML()
    
      Dim FileSpec As String
      Dim FSO As Object
      Dim HTMLCode As String
      Dim HTMLfile As Object
      Dim Rng As Range
      Dim Wks As Worksheet
      
        Set Wks = ActiveSheet
        
        Set Rng = Wks.Range("A1", Wks.Cells(Rows.Count, "A").End(xlUp))
        
          If Rng.Cells(1, 1) = "" Then Exit Sub
          FileSpec = CurDir & "\" & Rng.Cells(1, 1).Text & ".htm"
      
             'Convert the Range into HTML
              With ActiveWorkbook.PublishObjects
                .Add( _
                      SourceType:=xlSourceRange, _
                      FileName:=FileSpec, _
                      Sheet:=Wks.Name, _
                      Source:=Rng.Address, _
                      HtmlType:=xlHtmlStatic).Publish True
              End With
              
             'Read the HTML file back as a string
              Set FSO = CreateObject("Scripting.FileSystemObject")
              Set HTMLfile = FSO.OpenTextFile(FileSpec, 1, False)
                  HTMLCode = HTMLfile.ReadAll
                 'Left align HTML code
                  HTMLCode = Replace(HTMLCode, "align=center x:publishsource=", _
                             "align=left x:publishsource=")
              HTMLfile.Close
              
    End Sub

    Adding the Macro
    1. Copy the macro above pressing the keys CTRL+C
    2. Open your workbook
    3. Press the keys ALT+F11 to open the Visual Basic Editor
    4. Press the keys ALT+I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Paste the code by pressing the keys CTRL+V
    7. Make any custom changes to the macro if needed at this time.
    8. Save the Macro by pressing the keys CTRL+S
    9. Press the keys ALT+Q to exit the Editor, and return to Excel.

    To Run the Macro...
    To run the macro from Excel, open the workbook, and press ALT+F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
    Last edited by Leith Ross; 12-24-2010 at 08:07 PM. Reason: Added salutation
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  2. #2
    Registered User
    Join Date
    12-24-2010
    Location
    WV
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Macro to select cells and save as html

    Hi Leith,
    Thank you so much for providing this. However I couldnt get this to work for some reason. I am not sure if I wasnt able to describe what I really wanted or not. Any way, I got the below code from a friend which lets me select the range of data on the spreadseet (This sheet is called "datapage"). I have data in rows A1:H?. If I use this code below, I can select the dynamic range of data. Now the problem I have is that I need to be able to publish this selected range of data as web page and have the web page name to be derived from sheet called "Main" and cell A25. This cell actually already have the file name (ex 122410.htm). This name changes when I open the file so I just need the file name to reference to this cell.

    Hope this makes more sense and appreciate any help you can provide.

    Dim LastRow As Long
     
            'Search for any entry, by searching backwards by Rows.
            LastRow = Range("A1:h65536").Find(What:="*", After:=[A1], _
                  SearchOrder:=xlByRows, _
                  SearchDirection:=xlPrevious).Row
     
        Application.Goto Range("A1:H" & LastRow), True

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Macro to select cells and save as html

    Hello itmanusa,

    I made some changes to the macro. It now looks at columns "A:H" and finds the last entry. The file will now be created if it doesn't exist.
    'Written: December 24, 2010
    'Updated: December 24, 2010 - Extended the range and added auto file creation.
    'Author:  Leith Ross
    'Summary: Save the cells in column "A" staring with "A1" as an HTML file. The file is
    '         saved in the currrent directory and named using the contents of cell "A1".
    
    Sub SaveRangeAsHTML()
    
      Dim FileSpec As String
      Dim FSO As Object
      Dim HTMLCode As String
      Dim HTMLfile As Object
      Dim Rng As Range
      Dim RngEnd As Range
      Dim Wks As Worksheet
      
        Set Wks = ActiveSheet
        
        Set Rng = Wks.Range("A1:H" & Rows.Count)
        Set RngEnd = Rng.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False)
        Set Rng = Wks.Range(Rng.Cells(1, 1), Wks.Cells(RngEnd.Row, "H"))
        
        If Rng Is Nothing Then Exit Sub
        
          If Rng.Cells(1, 1) = "" Then Exit Sub
          FileSpec = CurDir & "\" & Rng.Cells(1, 1).Text & ".htm"
      
             'Convert the Range into HTML
              With ActiveWorkbook.PublishObjects
                .Add( _
                      SourceType:=xlSourceRange, _
                      FileName:=FileSpec, _
                      Sheet:=Wks.Name, _
                      Source:=Rng.Address, _
                      HtmlType:=xlHtmlStatic).Publish True
              End With
              
             'Read the HTML file back as a string. Create the file if needed.
              Set FSO = CreateObject("Scripting.FileSystemObject")
              Set HTMLfile = FSO.OpenTextFile(FileSpec, 1, True)
                  HTMLCode = HTMLfile.ReadAll
                 'Left align HTML code
                  HTMLCode = Replace(HTMLCode, "align=center x:publishsource=", _
                             "align=left x:publishsource=")
              HTMLfile.Close
              
    End Sub
    Last edited by Leith Ross; 12-25-2010 at 01:23 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1