Need help with vba code to create a table border.

Lidsavr

Active Member
Joined
Jan 10, 2008
Messages
330
I am writing code with Excel 2007 (Excel 2003 users sometimes run the code).

I think what I have is pretty simple, but I do not understand how it works. (I am trying to learn write code so it is clean (i.e, no excess data)).

An example of what I am trying to do is formatting the cell borders on a table:

I have an older VBA book that I reference and it says "The Borders collection contains four elements identified by these constants: xlTop, xlBottom, xlRight, and xlLeft. You can set the properties for these elements individually or all at once (J.Webb, 1996, p.358)." The book does not give an example of all at once and the macro recorder lists them individually (example):

Code:
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Color = RGB(0, 0, 0)
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Color = -8369125
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Color = -8369125
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Color = -8369125
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Color = -8369125
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Color = -8369125
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Range("A7:E7").Select
    Range("E7").Activate
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 8408091
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

I've tried rewriting the code to make it shorter, but I get errors and the code will not execute (on the line in red):

Code:
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
[COLOR="Red"]    With Selection.Borders(xlEdgeLeft, xlEdgeTop, xlEdgeBottom, xlEdgeRight, xlInsideVertical, xlInsideHorizontal)[/COLOR]        
        .LineStyle = xlContinuous
        .Color = RGB(0, 0, 0)
        .TintAndShade = 0
        .Weight = xlThin
    End With

How can I set the properties for the four elements "all at once" (like the author states)?

Thanks,

Lidsavr

References:
J.Webb. (1996). Special Edition Using Excel Visual Basic for Applications: Second Edition. QUE Publishing. Indianapolis, Indiana
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try like this

Code:
    With Selection.Borders
        .LineStyle = xlContinuous
        .Color = RGB(0, 0, 0)
        .TintAndShade = 0
        .Weight = xlThin
    End With
 
Upvote 0
VoG,
One again, I thank you! I did not know I could remove the element and the cell borders would all format together.

I have learned much from you over the past year and am looking forward to learning more in the future...

Respectfully,

Lidsavr
 
Upvote 0
You are welcome.

If you just want the defaults (and I don't think TintAndShade will work in Excel 2003)

Code:
    With Selection.Borders
        .Weight = xlThin
    End With
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

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