Vba Add Vertical Borders With An Exclusion

Lewzerrrr

Active Member
Joined
Jan 18, 2017
Messages
256
Hey,

I have a format data selection code. What I need now from it is to:
Add vertical borders across each column in the selected range.
Horizontal AND vertical across row(1) and last row of selected range.
Throughout the selected range, if a cell is blank I don't want a vertical border E.G. \ = vertical border. Notice Subtotal & Total both B4:C4 and B6:C6 are blank so they have no vertical borders.

[TABLE="width: 448"]
<tbody>[TR]
[TD="width: 64"][/TD]
[TD="width: 64"]A[/TD]
[TD="width: 64"]B[/TD]
[TD="width: 64"]C[/TD]
[TD="width: 64"]D[/TD]
[TD="width: 64"]E[/TD]
[TD="width: 64"]F[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[/TR]
[TR]
[TD="align: right"]3[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[/TR]
[TR]
[TD="align: right"]4[/TD]
[TD]\SUBTOTAL[/TD]
[TD]""[/TD]
[TD]""[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[/TR]
[TR]
[TD="align: right"]5[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]\TOTAL[/TD]
[TD]""[/TD]
[TD]""[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[TD]\6[/TD]
[/TR]
</tbody>[/TABLE]


Code:
Code:
Sub FormatData()

Dim Rng As Range


    Set Rng = Selection


    With Rng
    
        .Borders.LineStyle = xlNone
        .Font.Name = "Calibri"


        With .Rows(1)
            
            .Interior.ThemeColor = xlThemeColorAccent1
            .Interior.TintAndShade = -0.499984740745262
            .Font.Bold = True
            .Font.Size = 10
            .Font.ThemeColor = xlThemeColorDark1
            .WrapText = True
            
        End With


        With .Rows(2).Resize(.Rows.Count - 2)
        
            .FormatConditions.Delete
            .FormatConditions.Add Type:=xlExpression, Formula1:= _
                                  "=MOD(ROW(),2)=0"
            .FormatConditions(.FormatConditions.Count).SetFirstPriority


                With .FormatConditions(.FormatConditions.Count)
                
                    .Interior.PatternColorIndex = xlAutomatic
                    .Interior.ThemeColor = xlThemeColorAccent1
                    .Interior.TintAndShade = 0.799981688894314
                    
                End With
            
        End With
        
        With .Rows(2).Resize(.Rows.Count - 2)
        
            .Font.Size = 8
        
        End With


        With .Rows(.Rows.Count)
            .FormatConditions.Delete
            .Font.Bold = True
            .Font.ThemeColor = xlThemeColorDark1
            .Font.TintAndShade = 0
            .Font.Size = 10
            .Interior.ThemeColor = xlThemeColorAccent1
            .Interior.TintAndShade = -0.499984740745262
            .Interior.PatternTintAndShade = 0
        End With


    End With


End Sub
 
Last edited:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try adding the line in red
Code:
       With .Rows(2).Resize(.Rows.Count - 2)
        
            .Font.Size = 8
        
        End With

       [COLOR=#ff0000].SpecialCells(xlBlanks).Borders(xlEdgeLeft).LineStyle = xlNone[/COLOR]

        With .Rows(.Rows.Count)
            .FormatConditions.Delete
            .Font.Bold = True
            .Font.ThemeColor = xlThemeColorDark1
            .Font.TintAndShade = 0
            .Font.Size = 10
            .Interior.ThemeColor = xlThemeColorAccent1
            .Interior.TintAndShade = -0.499984740745262
            .Interior.PatternTintAndShade = 0
        End With
 
Upvote 0
Try adding the line in red
Code:
       With .Rows(2).Resize(.Rows.Count - 2)
        
            .Font.Size = 8
        
        End With

       [COLOR=#ff0000].SpecialCells(xlBlanks).Borders(xlEdgeLeft).LineStyle = xlNone[/COLOR]

        With .Rows(.Rows.Count)
            .FormatConditions.Delete
            .Font.Bold = True
            .Font.ThemeColor = xlThemeColorDark1
            .Font.TintAndShade = 0
            .Font.Size = 10
            .Interior.ThemeColor = xlThemeColorAccent1
            .Interior.TintAndShade = -0.499984740745262
            .Interior.PatternTintAndShade = 0
        End With

Tried this but no borders were added anywhere. I just typed up this code so will incorporate it in. Thanks so much :)

Code:
    For Each c In rng        If c <> "" Then
            With c.Borders
                .LineStyle = xlcontinous
                .Color = vbBlack
                .Weight = xlThin
            End With
        End If
    Next

Just seen the flaw in this ahaha, I would still need outside borders around all of the data.. I can add that :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top