Excel VBA Set Border on Cell: Step-by-Step Guide
In this article
You set a border on a cell in Excel VBA by using the Borders property of a Range object and choosing a line style, weight, and color. The shortest working version is Range("A1").Borders.LineStyle = xlContinuous, which puts a thin continuous border around all four sides of A1. To control individual edges, use Borders(xlEdgeTop), Borders(xlEdgeBottom), Borders(xlEdgeLeft), Borders(xlEdgeRight), or Borders(xlInsideHorizontal) and Borders(xlInsideVertical) for ranges. The rest of this guide shows exactly how to write, run, and troubleshoot that code.
What You'll Need
- Microsoft Excel for Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
- A workbook saved as .xlsm if you want to keep the macro; .xlsx will not store VBA code.
- Basic familiarity with opening the Visual Basic Editor (Alt+F11 on Windows, Option+F11 on Mac).
- A cell or range you want to format, for example A1 or A1:D10.
Menu labels can differ slightly by Excel version, device, and region, so if a label does not match exactly, look for the closest equivalent.
Step-by-Step
- Open the Visual Basic Editor. In Excel, press Alt+F11 (Windows) or Option+F11 (Mac). If nothing happens, enable the Developer tab first, then click Developer > Visual Basic.
- Insert a module. In the editor, click Insert > Module. A blank code window opens. Macros that format cells should live in a standard module, not in a worksheet module, unless you specifically want them to fire on an event.
- Write a simple macro. Type the following into the module:
This applies a thin continuous border to all four edges of A1.Sub AddBorder() Range("A1").Borders.LineStyle = xlContinuous End Sub - Control line weight and color. Extend the macro so the border is easier to see:
Sub AddBorderStyled() With Range("A1").Borders .LineStyle = xlContinuous .Weight = xlMedium .Color = RGB(0, 0, 0) End With End SubxlThin,xlMedium, andxlThickare the common weight constants.RGB(0, 0, 0)is black; swap the numbers for another color if you prefer. - Border only one edge. To underline a cell without boxing it in, target a single edge:
The edge constants areSub BottomBorderOnly() With Range("A1").Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlThin End With End SubxlEdgeTop,xlEdgeBottom,xlEdgeLeft,xlEdgeRight,xlDiagonalDown, andxlDiagonalUp. - Border a whole range, including inside lines. A range such as A1:D10 has an outer edge and internal grid lines. Use a loop over the six relevant border indexes:
SettingSub BorderRange() Dim b As Variant For Each b In Array(xlEdgeTop, xlEdgeBottom, xlEdgeLeft, xlEdgeRight, xlInsideHorizontal, xlInsideVertical) With Range("A1:D10").Borders(b) .LineStyle = xlContinuous .Weight = xlThin End With Next b End SubRange("A1:D10").Borders.LineStyle = xlContinuouswithout an index also applies borders to all sides, but the explicit loop makes it obvious which lines you are drawing. - Run the macro. Press F5 while the cursor is inside the subroutine, or close the editor and use Developer > Macros, select the macro name, and click Run. Switch back to the sheet to confirm the border appears.
- Remove borders when needed. Set the line style to
xlNoneto clear them:
This is the cleanest way to reset a range before applying a new style.Range("A1:D10").Borders.LineStyle = xlNone
Common Mistakes
- Forgetting that
Bordersis plural.Range("A1").Borderdoes not exist. Always useBorders. - Setting
ColorbeforeLineStyle. If the line style isxlNone, color and weight changes may not visibly apply. SetLineStylefirst, then weight and color. - Assuming
Borders.LineStyledraws inside lines. On a multi-cell range, the shorthand applies to the outer edges and to inside lines only when you address them explicitly withxlInsideHorizontalandxlInsideVertical. - Putting formatting code in a worksheet module. It will only run when the sheet event fires. Use a standard module for manual macros.
- Saving as .xlsx. The code is discarded. Save as .xlsm (macro-enabled workbook).
- Using the wrong constant name.
xlContinuous,xlDash,xlDot,xlDouble, andxlNoneare the common line styles. A misspelled constant raises a compile error.
Tips
- Use
Withblocks to set line style, weight, and color in one pass; it is shorter and less error-prone than repeatingRange(...).Borders(...). - Store the target range in a variable (
Dim r As Range: Set r = Range("A1:D10")) so you can reuse it and change it in one place. - If you want the border to follow the used range, use
ActiveSheet.UsedRange.Borders.LineStyle = xlContinuous, but be aware it formats every cell in the used area, including empty ones. - To avoid screen flicker on large loops, wrap the code in
Application.ScreenUpdating = Falseat the start and set it back toTrueat the end. - Use
xlThinfor grid lines andxlMediumorxlThickfor emphasis on totals and headers.
Troubleshooting
The macro runs but no border appears. Check that the range address is correct and that you are looking at the right sheet. If the cell already has a white border, a white border on white looks invisible; change the color to black and rerun.
You get a "Sub or Function not defined" error. A constant is misspelled or the module does not contain the subroutine. Recheck spelling of xlContinuous, xlEdgeBottom, and similar names, and confirm you are in a standard module.
"Object doesn't support this property or method." You likely used Border instead of Borders, or applied a border property to something that is not a Range, such as a Worksheet object.
The border disappears after saving. The workbook was saved as .xlsx. Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm).
Macros are blocked. Excel may show a security warning. Enable macros only for workbooks you trust; do not enable content from unknown sources. If the Developer tab is missing, enable it in Options as described above.
Borders look inconsistent across a range. Adjacent cells share edges, so setting a border on one cell can visually overlap the next. Apply the same style to the whole range in one call rather than cell by cell.
Frequently Asked Questions
How do I put a border around a single cell in VBA?
Use Range("A1").Borders.LineStyle = xlContinuous. That applies a thin line to all four edges of the cell.
How do I add only a bottom border?
Target the bottom edge: Range("A1").Borders(xlEdgeBottom).LineStyle = xlContinuous. Add .Weight = xlThin if you want to be explicit about thickness.
How do I remove all borders from a range?
Set the line style to none: Range("A1:D10").Borders.LineStyle = xlNone. This clears every edge and inside line in the range.
Can I set different borders on different sides?
Yes. Address each edge separately, for example Borders(xlEdgeTop) with xlDouble and Borders(xlEdgeBottom) with xlThin. Each edge has its own line style, weight, and color.
Why does my border code fail on a merged cell?
Merged cells behave differently. Apply the border to the merged range as a whole, such as Range("A1:C1").Borders.LineStyle = xlContinuous, rather than to individual cells inside the merge.
Does this work in Excel for Mac?
Yes, the VBA object model for borders is the same. The keyboard shortcut to open the editor differs (Option+F11), and the Developer tab is enabled through Excel > Preferences rather than File > Options.