+ Reply to Thread
Results 1 to 9 of 9

If # > 0 in $D4 then $A4 thru $D4 BorderAround

Hybrid View

  1. #1
    Registered User
    Join Date
    01-30-2014
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    22

    If # > 0 in $D4 then $A4 thru $D4 BorderAround

    Hi!
    Could you help me write a code to add borders around information in Cells A-D.
    The workbook I'm attaching has the top example without the borders and the bottom example with the borders for a visual.
    Please note, there will always be a number in column D if that row needs borders.

    borders.xlsx

    If conditional formatting would work better for this, please let me know. I can't figure out if conditional formatting can do outside borders around more than one cell on a row.

    Thanks in advance!
    Klotzy

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: If # > 0 in $D4 then $A4 thru $D4 BorderAround

    Regular CF will work just fine for this, no VBA needed.

    1. highlight the range you want to apply the conditional formatting to (A4:E5 in your sample)
    2. on the home tab, styles, select CF
    3. select new rule, select use formula
    4. enter =$D4>0
    Format Border Outline
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Registered User
    Join Date
    01-30-2014
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: If # > 0 in $D4 then $A4 thru $D4 BorderAround

    Thanks FDibbins for the quick reply.
    Unfortunately, I can't have each cell bordered, only the outline. For example, the vertical border line between cells A and B can't be there.

    I'm hoping to select an entire sheet full of these Packing Lists and have A:D Bordered only when there is a number in column D. The Packing List header will not get a border.

    Do you have more advice?
    Thanks again,
    Klotzy

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: If # > 0 in $D4 then $A4 thru $D4 BorderAround

    OK, that just means a few more rules.

    Use what I gave you as the 1st rule (across the entire range), but change the border to just top and bottom - you will need to "unclick" the left and right borders
    Then create a new rule that ONLY applies to the A4:A5 range, use the exact same formula and under Format/Border, click the LEFT border
    Then repeat that for the right border

  5. #5
    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: If # > 0 in $D4 then $A4 thru $D4 BorderAround

    Hello klotzy,

    Welcome to the Forum!
    I don't think conditional formatting is a good option for you. If you will have many packing slips per page and they will go across as well as down the sheet then conditional formatting would not be a good choice.

    Here is a VBA macro that will find each packing slip on the sheet and add the borders to only the cells that need them. This has been added to the attached workbook. There is button on the sheet to run the macro.
    Sub BorderAround()
    
        Dim Cell    As Range
        Dim Rng     As Range
        Dim Row     As Long
        Dim Start   As String
        Dim Wks     As Worksheet
        
            Set Wks = ActiveSheet
            
            Set Cell = Wks.Cells.Find("Packing Slip", , xlValues, xlWhole, xlByRows, xlNext, False, False, False)
            
            If Cell Is Nothing Then Exit Sub
            
                Start = Cell.Address
                
                Do
                    Set Rng = Cell.CurrentRegion
                    Set Cell = Wks.Cells.FindNext(Cell)
                        For Row = 4 To Rng.Rows.Count
                            If Rng.Cells(Row, "D") > 0 Then
                                Rng.Rows(Row).BorderAround xlContinuous, xlThin
                            End If
                        Next Row
                    If Cell Is Nothing Then Exit Do
                    If Cell.Address = Start Then Exit Do
                Loop
            
    End Sub
    Attached Files Attached Files
    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!)

  6. #6
    Registered User
    Join Date
    01-30-2014
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: If # > 0 in $D4 then $A4 thru $D4 BorderAround

    Thanks FDibbins for your help - I appreciate the tips.
    Leith Ross - I agree that VBA will be more fitting to my needs, thanks very much for the code.
    Can you please tell me how to alter the border to only border around A:D instead of A:the last used cell in the row?
    Does it do that because you declared Row as Long?
    Dim Row     As Long
    I appreciate your help!
    Thanks!
    Klotzy

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: If # > 0 in $D4 then $A4 thru $D4 BorderAround

    FWIW:

    Sub klotzy()
    Dim rcell As Range
    For Each rcell In Range("D2:D" & Range("D" & Rows.Count).End(3).row)
        If IsNumeric(rcell) And Len(rcell) > 0 Then
            With Range(Cells(rcell.row, "A"), Cells(rcell.row, "D"))
                .Borders(xlDiagonalDown).LineStyle = xlNone
                .Borders(xlDiagonalUp).LineStyle = xlNone
                With .Borders(xlEdgeLeft)
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                    .ColorIndex = xlAutomatic
                End With
                With .Borders(xlEdgeTop)
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                    .ColorIndex = xlAutomatic
                End With
                With .Borders(xlEdgeBottom)
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                    .ColorIndex = xlAutomatic
                End With
                With .Borders(xlEdgeRight)
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                    .ColorIndex = xlAutomatic
                 End With
                .Borders(xlInsideVertical).LineStyle = xlNone
            End With
        End If
    Next rcell
    End Sub

  8. #8
    Registered User
    Join Date
    01-30-2014
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: If # > 0 in $D4 then $A4 thru $D4 BorderAround

    Thank you so much John H Davis! That's perfect!

    You all are the best!

  9. #9
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: If # > 0 in $D4 then $A4 thru $D4 BorderAround

    You're welcome. Glad to help out and thanks for the feedback.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] BorderAround method failure
    By johnhildreth@citynet.net in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-18-2005, 02:50 PM

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