+ Reply to Thread
Results 1 to 6 of 6

Macro to select cells and save as html

Hybrid View

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

    Thumbs up Macro to select cells and save as html

    HI guys,
    I am hoping that some one can help me with this. Have been looking everywhere and cant seem to get the right answer.

    Here is what I have:

    Sheet1 has several rows of data then one empty row. Then I have several rows of data and an empty row. This continues for about 100-150 times (this varies each week)

    Here is what I am trying to do.

    I need the macro to be able to select from A1 to the last row:column that has data so that I can apply borders (keep in mind that last row:column will always not be the same cell since number of rows varies).

    Then I would like to save the selection as html file and file name should be whatever value I have on a1 (so if I have Jan012011 in A1, it needs to be Jan012011.htm)

    I would greatly appreciate if anyone could help me with that. I have found couple of vbscript to select the cells, however because I have empty rows, those only selects first set of rows. I need the script to go all the way to the last cell that has any data.

    Thanks for your help
    Last edited by itmanusa; 12-27-2010 at 09:21 AM. Reason: Solved

  2. #2
    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!)

  3. #3
    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

  4. #4
    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.

  5. #5
    Registered User
    Join Date
    12-10-2010
    Location
    Hong Kong
    MS-Off Ver
    Excel 97
    Posts
    13

    Question Re: Macro to select cells and save as html

    Hi, Leith Ross.
    Excuse me, I have a few questions to ask.
    Because I wonder I use some 'old style' of coding to do.
    What is the difference and which can be easier on debug?

    If user want to cap more the one column with different type of HTML tag show.
    such as Column B to be Date Format YYYY/MM/DD, Column C to be Red in color in -ve.

    As you see, my code as if MS-DOS aged..

    ' to Count the # of Column A
    AA = Application.CountA(Range("A:A"))
    
    ' do loop to step one by one on HTML tag string
    HTMLStr = "<HTML>" + CHR(13)
    BB = 0
    Do
       HTMLStr = HTMLStr + ....
       BB = BB+1
    Loop Until BB=AA
    ...
    
    Function SaveTextFile(strFile As String, strData As String, Optional bOverWrite As Boolean = False) As Boolean
        Dim iHandle As Integer, l As Long
        If Not bOverWrite Then
            If Len(Dir(strFile)) > 0 Then
                SaveTextFile = False
                Exit Function
            End If
        Else
            If Len(Dir(strFile)) > 0 Then
                'Delete Before write
                Kill (strFile)
            End If
        End If
        iHandle = FreeFile
        l = Len(strData)
        If l > 32766 Then
            l = 32766
        End If
        Open strFile For Binary Access Write As #iHandle Len = l
        Put #iHandle, , strData
        Close #iHandle
        SaveTextFile = True
    End Function

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

    Thumbs up [SOLVED] Macro to select cells and save as html

    Leith,
    I would like to thank you once again for the code. It worked just as I needed it to work. I did have to make a little change for the file to be saved. I had to hard code the destination location, for some reason the current directory location only worked once. Every time after that I couldn't get it to work until I typed the whole path.

    Once again thanks a lot and happy new year to you.

    Your code:
    FileSpec = CurDir & "\" & Rng.Cells(1, 1).Text & ".htm"
    I changed to:
    FileSpec = "c:\folder\" & Rng.Cells(1, 1).Text & ".htm"

+ 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