Free Support Forum -

Merged Cell Border Problems

I"m creating a spreadsheet with a lot of merged cells that need to have borders. When I set the border, it only appears on the first (upper left) cell in the merged area. Here’s code that demonstrates this:

Dim oCell As Cell = xl.Worksheets(0).Cells(5, 5)

oCell.Style.Borders(BorderType.BottomBorder).LineStyle = CellBorderType.Medium

oCell.Style.Borders(BorderType.LeftBorder).LineStyle = CellBorderType.Medium

oCell.Style.Borders(BorderType.RightBorder).LineStyle = CellBorderType.Medium

oCell.Style.Borders(BorderType.TopBorder).LineStyle = CellBorderType.Medium

xl.Worksheets(0).Cells.Merge(5, 5, 3, 3)

I’m using version


Hi Ethan,

You should set all merged cells’ borders. The following is the sample code:

Dim rng As Range = xl.Worksheets(0).Cells.CreateRange(5,5,3,3)
rng.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Medium, Color.Black)
rng.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Medium, Color.Black)
rng.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Medium, Color.Black)
rng.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Medium, Color.Black)

xl.Worksheets(0).Cells.Merge(5, 5, 3, 3)