Forum Discussion

Unlucky2's avatar
Unlucky2
Copper Contributor
Oct 26, 2022

VBA Macro to create borders dynamically

Hello Guys,

I need your help because I'm stuck.

I have excel with monthly timetable with days above days. Those days are created automatically through VBA script based on date.

What I also need he is VBA to create borders (Left and right bor

  • SnowMan55's avatar
    SnowMan55
    Bronze Contributor

    Unlucky2 

    Here is sample code for you to experiment with in another worksheet, before you try doing something similar as part of (or called from) your current macro:

    Sub SettingBorders()
        With Range("B12:C14")
            With .Borders(XlBordersIndex.xlEdgeLeft)
                .LineStyle = XlLineStyle.xlContinuous
                .TintAndShade = 0
                .Weight = XlBorderWeight.xlMedium
            End With
            With .Borders(XlBordersIndex.xlEdgeRight)
                .LineStyle = XlLineStyle.xlDouble
                .Color = vbRed
                .TintAndShade = 0
                .Weight = XlBorderWeight.xlThick
            End With
        End With
    End Sub

    Those "XlBordersIndex.", "XlLineStyle.", and "XlBorderWeight." qualifiers are not required, but they are useful with the VBA editor's Intellisense feature.  (Each of those qualifier names is an enumeration -- a set of numbers that have names and a related meaning.)  To try the other options (enumeration members) available: Delete the name that follows the period, and delete the period also.  Retype the period, and you will see a dropdown list of the valid options (the other enumerations); click one to select it (or first type a few characters if you want to scroll to a different part of the list).  Rerun the procedure.

Resources