Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Macro to produce Thick Border Box

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    40
    Rep Power
    0

    Macro to produce Thick Border Box

    I would like a think border box to be automatically inserted where there are values to the right of text which is in Col A and the text is in bold

    For Eg if A15 "Net Profit" is in in Bold and B15, C16, D17 etc has values then a thick Border must be inserted around the values

    If the text is in Bold and the first row next to it is blank, then this must be ignored i.e no thick black box where there are no values to the right is text that is in bold

    Your assistance in this regard is most appreciated

  2. #2
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    12
    Did you use the macrorecorder ?

  3. #3
    Member
    Join Date
    Aug 2012
    Posts
    40
    Rep Power
    0
    Thanks for the reply

    I did use a macro recorder, but not sure how to have the thick Border Box aroond the values in the same row where the text in Col, A is highlighted
    I have attempted to write code but need help in completing this

    Will most propably have to have this as a Private Sub to be copied in the code for the applicable sheet

    Code:
    Sub Borders ()
    
    If Text.bold = True 
    Then  ?????
    
    With Selection.Borders(xlEdgeLeft)
             .LineStyle = xlContinuous
    
    
    Else 
    Exit Sub
    next 
    
    End with
    End Sub
    Your assistance in resolving this is most appreciated

  4. #4
    Senior Member alansidman's Avatar
    Join Date
    Apr 2012
    Posts
    125
    Rep Power
    13
    try this:

    Code:
    Option Explicit
    
    Sub bold()
    Dim lr As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    Dim c As Range
    Dim rng As Range
    Set rng = Range("A1:A" & lr)
    
    For Each c In rng
    If c.Font.bold = True And Len(c.Offset(0, 1)) > 0 Then
        c.Resize(, 5).Select
        Selection.BorderAround Weight:=xlThick
    End If
    Next c
    MsgBox "complete"
    End Sub

  5. #5
    Member
    Join Date
    Aug 2012
    Posts
    40
    Rep Power
    0
    Thanks for the help, much appreciated

  6. #6
    Member
    Join Date
    Aug 2012
    Posts
    40
    Rep Power
    0
    Hi Alan

    Code works perfectly where there are several columns next to Col A where the text is in bold

    I have another sheet where there is only one value next to the text which is in bold and I get type mismatch and the following code is highlighted

    Code:
     If c.Font.bold = True And Len(c.Offset(0, 1)) > 0 Then
    I have amended your code to produce a thick border box where there is is one value to the right of the bold text

    Kindly test & amend

    Code:
     Sub Thick_Border_Bank()
    Sheets("Man Accounts Bank").Select
    Dim lr As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    Dim c As Range
    Dim rng As Range
    Set rng = Range("A1:A" & lr)
    
    For Each c In rng
    If c.Font.bold = True And Len(c.Offset(0, 1)) > 0 Then
         c.Offset(, 1).Select
        Selection.BorderAround Weight:=xlThick
    End If
    Next c
    MsgBox "complete"
    End Sub

  7. #7
    Senior Member alansidman's Avatar
    Join Date
    Apr 2012
    Posts
    125
    Rep Power
    13
    Tested. Worked without issue. Had to change the sheet name to reflect my sheet name, but other than that, I used the code provided without any problems. If you would like further help, suggest you upload your problem workbook/worksheet for analysis.

  8. #8
    Member
    Join Date
    Aug 2012
    Posts
    40
    Rep Power
    0
    Hi Alan

    I added "On Error resume next" to mey code and it now works

  9. #9
    Junior Member
    Join Date
    Aug 2014
    Posts
    10
    Rep Power
    0
    There is no need to Select the cells before you put the border around them, and as you only use some of those values once you don't need to assign them to variables and the code will be much shorter:

    Code:
    Sub Thick_Border_Bank()
    Sheets("Man Accounts Bank").Select
    Dim c As Range
    For Each c In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
        If c.Font.Bold = True And c.Offset(, 1) <> "" Then c.Offset(, 1).BorderAround , xlThick
    Next c
    MsgBox "complete"
    End Sub

  10. #10
    Moderator
    Join Date
    Jul 2012
    Posts
    156
    Rep Power
    12
    There's not even need to select the sheet.
    Code:
    Sub Thick_Border_Bank()
        Dim c As Range
        With Sheets("Man Accounts Bank")
            For Each c In .Range("A1:A" & .Cells(.Rows.Count, 1).End(xlUp).Row)
                If c.Font.Bold And c.Offset(, 1) <> vbNullString Then c.Offset(, 1).BorderAround , xlThick
            Next c
        End With
    End Sub

Similar Threads

  1. List box tabbing
    By RedJames in forum Excel Help
    Replies: 1
    Last Post: 06-18-2014, 04:14 PM
  2. Replies: 3
    Last Post: 06-01-2013, 11:31 AM
  3. Excel VBA Macro To Open A File Through Browse Dialog Box
    By Safal Shrestha in forum Excel Help
    Replies: 2
    Last Post: 04-05-2013, 12:59 PM
  4. OpenFileDialog box malfunctions
    By hometech in forum Word Help
    Replies: 1
    Last Post: 10-22-2012, 04:15 PM
  5. Setting up a check box
    By Howardc in forum Excel Help
    Replies: 2
    Last Post: 08-07-2012, 08:26 AM

Posting Permissions

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