MrExcel Publishing
Your One Stop for Excel Tips & Solutions

VB Code to Select Active Cells and Format a Border?


Posted by Mark Israel on July 02, 2001 11:45 AM

I would like a macro I'm using to select all active cells and then format those cells with a black border. Any advice?

Thanks,
Mark


Posted by Ben O. on July 02, 2001 12:14 PM

By active cells I assume you mean all the cells currently selected. I think there can only be one active cell. This code should work:

Sub FormatBorders()
For x = 7 To 10
With Selection.Borders(x)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 1
End With
Next x
End Sub

Change "7 to 10" to "1 to 4" if you want inner and outer borders.

-Ben

Posted by Mark Israel on July 02, 2001 12:18 PM

Actually, by active, I meant all cells with data in them. So, I want the macro to do the selection of all cells with data and then place a border around those cells.

Thanks,
Mark

Posted by Russell on July 02, 2001 6:24 PM

You could also use conditional formatting, negating the need for VBA. Just set the conditional format to Cell is not equal to: -- and select an empty cell (one that you know will always be empty)....

You could just format your whole area like this, and it will put a border around those cells that have values in them.