Cell and range formatting issue


I format a given range by using a style object as follows:

oSheet.Cells.CreateRange(“A1”, “BJ10”).Style = styleBase

…where styleBase is the style object but has no border properties set.

Then, I try to set the border of a specific cell within that range:

oSheet.Cells(“H5”).Style.Borders(BorderType.BottomBorder).LineStyle = CellBorderType.Thin
oSheet.Cells(“H5”).Style.Borders(BorderType.BottomBorder).Color = Color.Orange

However, when I do this the ENTIRE range of cells is changed, instead of the specific cell.
I’ve attached a graphic of the formatting I need… any help appreciated.

I’m formatting cells using style objects for fonts and alignments etc. I’m then trying to set border formats for specific cells and ranges after that.


When you use Range.Style to set styleBase to this range, all cells in this range contain the same style object. So if you change one of them, all formattings are changed.

You can change your code to:

oSheet.Cells.CreateRange(“A1”, “BJ10”).Style = styleBase

Dim newStyle as Style = excel.Styles(excel.Styles.Add())
newStyle.Borders(BorderType.BottomBorder).LineStyle = CellBorderType.Thin
newStyle.Borders(BorderType.BottomBorder).Color = Color.Red

oSheet.Cells(“H5”).Style = newStyle



This seems to have worked a treat! Big Smile