Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove borders from cells in a range in Excel using VB.net?

Aim to Achieve: To get rid of borders if any in the cells of range.

I have :

Dim range As Excel.Range = sheet.Range("A2:K100")
For Each cell In range
    // Some cells in the Range has borders
    // How to remove borders from cells in the range
Next cell

Please help.. !

I am new to Vb.net !

like image 705
Yugal Jindle Avatar asked Aug 07 '11 18:08

Yugal Jindle


People also ask

How do I remove cell borders in Excel VBA?

Removing borders is similar to creating borders. All you have to do is set the . LineStyle property to xlNone.

How do you remove borders from selected cells?

On a worksheet, select the cell or range of cells that you want to remove a border from. To cancel a selection of cells, click any cell on the worksheet. Click Home > the Borders arrow > Erase Border, and then select the cells with the border you want to erase.

What tool is used to apply borders format to a range of cells?

Another way to add borders around cells is to use the Borders tool on the toolbar. You can use this tool by following these steps: Select the cell or range of cells that you want bordered. If the type of border you want applied appears on the Borders tool, click on it and you are finished.


2 Answers

range.Borders(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlLineStyle.xlLineStyleNone
range.Borders(Excel.XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlLineStyle.xlLineStyleNone
range.Borders(Excel.XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlLineStyle.xlLineStyleNone
range.Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlLineStyleNone
range.Borders(Excel.XlBordersIndex.xlInsideHorizontal).LineStyle = Excel.XlLineStyle.xlLineStyleNone
range.Borders(Excel.XlBordersIndex.xlInsideVertical).LineStyle = Excel.XlLineStyle.xlLineStyleNone

Removes the borders around the cells and between cells (via xlInsideHorizontal and xlInsideVertical). If you expect diagonal borders, include xlDiagonalDown and xlDiagonalUp.

Okay, the above code was very verbose. The following should do it too:

For Each border in range.Borders
    border.LineStyle = Excel.XlLineStyle.xlLineStyleNone
Next

See: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.borders.aspx

EDIT:

While looking over the MSDN page, I'm wondering if this one liner could do it too:

range.Borders.LineStyle = Excel.XlLineStyle.xlLineStyleNone
like image 72
VVS Avatar answered Oct 17 '22 12:10

VVS


Range("A2:K100").Borders.LineStyle = xlNone

like image 37
Unknown Avatar answered Oct 17 '22 12:10

Unknown