20

I am trying to select all the cells in an Excel sheet in the Excel.Range object of C# for applying auto fit, border etc. I have some merged cells within the sheets.

Is there any simple trick to do so?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Chethan
  • 360
  • 1
  • 2
  • 15

10 Answers10

16
Excel.Range theRange = (Excel.Range)CurrentSheet.UsedRange;

In this example, CurrentSheet is the variable where you have stored the sheet you are currently using.

ThunderGr
  • 2,297
  • 29
  • 20
  • 1
    Although the OP actually asked for "all cells", this is what I, and I suspect most other users were actually looking for. – primo Mar 06 '21 at 06:14
14
public void refreshSheetColumsSize(Worksheet ws)
 {
    ws.get_Range("a1").EntireRow.EntireColumn.Select();         
 }
jocelyn
  • 788
  • 6
  • 12
  • 1
    Looks nice, but i got this exception `Cannot implicitly convert type 'bool' to 'Microsoft.Office.Interop.Excel.Range'` – Yola Mar 16 '15 at 07:28
  • get_range should return a range and not a bool : https://msdn.microsoft.com/fr-fr/library/microsoft.office.tools.excel.worksheet.get_range.aspx call ws.get_Range("a1"); in a separate row and debug the result. – jocelyn Mar 17 '15 at 09:22
  • 1
    removing the `Select()`will do it. I was getting the same exception as Yola. I mean, create a method that returns a `Range`and return `get_Range("a1").EntireRow.EntireColumn` – Veverke Jun 27 '16 at 16:06
12

Taken from here, this will select all cells in the worksheet:

lastCol = ActiveSheet.Range("a1").End(xlToRight).Column
lastRow = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row
ActiveSheet.Range("a1", ActiveSheet.Cells(lastRow, lastCol)).Select
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
3

Officially, Excel.Worksheet.UsedRange.Rows and Excel.Worksheet.UsedRange.Columns.

In practice, it's buggy, you have to subtract the start row and column. The closest-to-correct answer is:

   Public ReadOnly Property LastColumn() As Integer
        Get
            Return ExcelWorksheet.UsedRange.Columns.Count + _
                   ExcelWorksheet.UsedRange.Column - 1
        End Get
    End Property
    Public ReadOnly Property LastRow() As Integer
        Get
            Return ExcelWorksheet.UsedRange.Rows.Count + _
                   ExcelWorksheet.UsedRange.Row - 1
        End Get
    End Property

This returns at least all the used cells, sometimes a little more. The 'little more' can be due to blank cells (rather than empty), and other random things. Form the research I did, this is the best that can be done.

If you really want to select everything then

ExcelWorksheet.Activate()
ExcelWorksheet.Cells.Select()
smirkingman
  • 6,167
  • 4
  • 34
  • 47
2

To consider all cells of a worksheet you can write like:

workSheet.Cells[workSheet.Rows.Count,workSheet.Columns.Count]

OR

 workSheet.get_Range("A1","IV65536")

To consider used cells of a worksheet, you can write:

workSheet.Rows.SpecialCells(XlCellType.xlCellTypeLastCell, XlSpecialCellsValue.xlTextValues)

Where "worksheet" represents the sheet you are working on. Both code sample returns a range.

Hope it helps!

NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
2

I've not done any excel development for a while (Excel 2003) but I always found that recording a macro while performing the tasks I was wanting to implement in code have sufficient pointers to help.

In this case, selecting all cells and autofitting gives the code:

Sub Macro1()
    Cells.Select
    Cells.EntireColumn.AutoFit
End Sub

which I would imagine would roughly translate to:

((Microsoft.Office.Interop.Excel.Range)_sheet.Cells.Select()).AutoFit();

where _sheet is the instance of the Worksheet you're using. (untested)

Dave
  • 3,581
  • 1
  • 22
  • 25
1

Much cleaner and doesn't depend on the number of rows/cols which were increased in Excel 2007:

Provided your sheet is in a variable called wsData:

wsData.Range(wsData.Cells(1, 1), wsData.Cells(wsData.Rows.Count, wsData.Columns.Count))
Chris
  • 11
  • 1
0

Very simple:

xlWorkSheet.UsedRange.Columns.Select()
Lev Z
  • 742
  • 9
  • 17
0
worksheet.Columns.AutoFit()

Where "worksheet" is a variable of type Worksheet

pmartin
  • 2,731
  • 1
  • 25
  • 30
0
        xlWorksheet.get_Range("a1").EntireRow.EntireColumn.AutoFit();
        xlWorksheet.get_Range("a1").EntireColumn.EntireRow.AutoFit();

After cell filling has been finished.