+ Reply to Thread
Results 1 to 7 of 7

VBA to add a double line bottom border based on cell value

Hybrid View

KDF VBA to add a double line... 01-09-2013, 05:10 PM
Andrew-R Re: VBA to add a double line... 01-09-2013, 05:27 PM
KDF Re: VBA to add a double line... 01-09-2013, 05:33 PM
Andrew-R Re: VBA to add a double line... 01-09-2013, 05:42 PM
KDF Re: VBA to add a double line... 01-09-2013, 05:51 PM
Andrew-R Re: VBA to add a double line... 01-09-2013, 06:08 PM
KDF Re: VBA to add a double line... 01-09-2013, 06:16 PM
  1. #1
    Registered User
    Join Date
    12-20-2012
    Location
    Dallas, TX
    MS-Off Ver
    Excel2010
    Posts
    21

    VBA to add a double line bottom border based on cell value

    I need some code to place a bottom doulbe line border in a range of cells based on another cell's value. In other words, if cell C2 = 10, I need a double bottom line border in cells C15 to L15. This border needs to change as C2 changes. For example, if C2 were to change to 5, then the double bottom line border would have to now only be in cells C15 to G15. Any help would be greatly appreciated. Thanks.

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: VBA to add a double line bottom border based on cell value

    You don't need VBA, you can do this far easier with conditional formatting:

    Select all of row 15, add a new conditional format, based on a formula, and use the formula:

    =AND(COLUMN(A15)>=3,COLUMN(A15)<=2+$C$5)

    And then set the format to be double underlined.

  3. #3
    Registered User
    Join Date
    12-20-2012
    Location
    Dallas, TX
    MS-Off Ver
    Excel2010
    Posts
    21

    Re: VBA to add a double line bottom border based on cell value

    Thanks for the reply. I tried that but the conditional formatting doesn't allow for a double line bottom border, just a single line.

  4. #4
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: VBA to add a double line bottom border based on cell value

    Ah, sorry, misread your post and though you were after a double-underline.

    Have some VB by way of an apology

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Const sTRIGGER_CELL As String = "C5"
    Const sSTART_CELL As String = "C15"
    
    If Not Intersect(Target, Range(sTRIGGER_CELL)) Is Nothing Then
      Range(Range(sSTART_CELL), Cells(Range(sSTART_CELL).Row, Columns.Count)).Borders(xlEdgeBottom).LineStyle = xlNone
      Range(Range(sSTART_CELL), Range(sSTART_CELL).Offset(0, Range(sTRIGGER_CELL).Value - 1)).Borders(xlEdgeBottom).LineStyle = xlDouble
    End If
    
    End Sub

  5. #5
    Registered User
    Join Date
    12-20-2012
    Location
    Dallas, TX
    MS-Off Ver
    Excel2010
    Posts
    21

    Re: VBA to add a double line bottom border based on cell value

    You're the greatest. One last question: what if the trigger cell is on a different sheet?

  6. #6
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: VBA to add a double line bottom border based on cell value

    The worksheet change event needs to be behind the sheet containing the trigger cell, but then we can specify another sheet as the target:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Const sTRIGGER_CELL As String = "C5"
    Const sSTART_CELL As String = "C15"
    Const sTARGET_SHEET As String = "Sheet3"
    
    If Not Intersect(Target, Range(sTRIGGER_CELL)) Is Nothing Then
      With Sheets(sTARGET_SHEET)
        .Range(.Range(sSTART_CELL), .Cells(.Range(sSTART_CELL).Row, .Columns.Count)).Borders(xlEdgeBottom).LineStyle = xlNone
        .Range(.Range(sSTART_CELL), .Range(sSTART_CELL).Offset(0, Range(sTRIGGER_CELL).Value - 1)).Borders(xlEdgeBottom).LineStyle = xlDouble
      End With
    End If
    
    End Sub

  7. #7
    Registered User
    Join Date
    12-20-2012
    Location
    Dallas, TX
    MS-Off Ver
    Excel2010
    Posts
    21

    Re: VBA to add a double line bottom border based on cell value

    This is great. Thanks a million, Andrew-R, you're the best.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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