+ Reply to Thread
Results 1 to 9 of 9

Thread: Macro to apply borders to all cells with text

  1. #1
    Registered User
    Join Date
    01-22-2009
    Location
    Okinawa, Japan
    MS-Off Ver
    Excel 2003
    Posts
    5

    Macro to apply borders to all cells with text

    I have been looking everywhere for an example on how to apply+ borders to all cells in a worksheet with text.

    I pull reports from a program and sometimes there are blank cells throughout the row. Currently I have to manually highlight all cells with text and press the border button. It would save a lot of time if I could just run a macro that would put borders around all cells that have text.

    Is this possible?

    Thank you!
    Last edited by VBA Noob; 01-22-2009 at 12:57 PM.

  2. #2
    Forum Guru
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2003
    Posts
    2,173
    This will put borders around each individual cell that has text. Is that what you are looking for?

    Sub DrawBorders()
        With Cells.SpecialCells(xlCellTypeConstants, 23)
            .BorderAround xlContinuous, xlThin, xlColorIndexAutomatic
            On Error Resume Next  'used in case there are no inside borders
            With .Borders(xlInsideHorizontal)
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
            With .Borders(xlInsideVertical)
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
        End With
    End Sub
    HTH

    Jason

  3. #3
    Registered User
    Join Date
    01-22-2009
    Location
    Okinawa, Japan
    MS-Off Ver
    Excel 2003
    Posts
    5

    Question 50% Solution

    Jason,

    Thanks for the quick reply. When I ran the macro it formatted every cell from left to right that had text in that row.

    Here is an example of the spreadsheet I have:

    Text are in the following cells:

    A1 B1 C1 D1
    D2
    B3 D3

    I am just looking for those cells to receive the borders. I do not want borders in the others (i.e.A2, A3, B2)

  4. #4
    Forum Guru
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2003
    Posts
    2,173
    Can you post up an example copy of your workbook so I can take a look at it?

  5. #5
    Registered User
    Join Date
    01-22-2009
    Location
    Okinawa, Japan
    MS-Off Ver
    Excel 2003
    Posts
    5

    Example

    Here is an example of the worksheet after I format it.
    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2003
    Posts
    2,173
    Ok, so in your example, what did you expect to accomplish? Do you want borders only in cells that have letters? Or only that have words? Please be more specific in exactly what you are looking for.

  7. #7
    Registered User
    Join Date
    01-22-2009
    Location
    Okinawa, Japan
    MS-Off Ver
    Excel 2003
    Posts
    5
    I need the macro to put borders around all cells with text inside. Just like how I have the borders formatted in the sample worksheet I sent you. If the cell does not have text, it does not get a border. If it has text it gets a border.

  8. #8
    Forum Guru
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2003
    Posts
    2,173
    The issue is that several of the cells have spaces in them. So you first need to get rid of those, then apply the borders. Try:

    Sub DrawBorders()
        Dim Cell As Range
        
        For Each Cell In Cells.SpecialCells(xlCellTypeConstants, xlTextValues)
            If Left(Cell, 2) = "  " Then Cell.ClearContents
        Next Cell
        
        With Cells.SpecialCells(xlCellTypeConstants, 23)
            .BorderAround xlContinuous, xlThin, xlColorIndexAutomatic
            On Error Resume Next  'used in case there are no inside borders
            With .Borders(xlInsideHorizontal)
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
            With .Borders(xlInsideVertical)
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
        End With
    End Sub
    HTH

    Jason

  9. #9
    Registered User
    Join Date
    01-22-2009
    Location
    Okinawa, Japan
    MS-Off Ver
    Excel 2003
    Posts
    5

    Thumbs up Solved

    Thanks for the help. You saved me a lot of time.

+ 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.2.0