0

I am having an issue that I am sure is silly and I am missing the obvious. I get the following error when I put "wksht.Cells[1,1].Select;" into the code to select the upper most left hand cell.

Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement.

here is the Method its being called in:

        public void ValueSaves(Excel.Workbook wb, Excel.Application excelApp)
    {
        Excel.Worksheet wksht;
        excelApp.ScreenUpdating = false;


        for (int i = 0; i < lstSaveSheet.Items.Count; i++)
        {
            string range = lstSaveRange.Items[i].ToString();
            wksht = wb.Sheets[lstSaveSheet.Items[i].ToString()];
            Boolean sheetProtected = false;

            if (wksht.ProtectContents)
            { sheetProtected = true; }

            wksht.Unprotect();
            if (range == "whole sheet")
            {
                //select & copy whole sheet
                wksht.get_Range("a1").EntireRow.EntireColumn.Copy(Type.Missing);

                //paste whole sheet as a value
                wksht.get_Range("a1").EntireRow.EntireColumn.PasteSpecial(Excel.XlPasteType.xlPasteValues, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
            }
            else
            {
                string[] CellRefs = lstSaveRange.Items[i].ToString().Split(':');
                wksht.get_Range(CellRefs.First(), CellRefs.Last()).Copy(Type.Missing);

                //paste range as a value
                wksht.get_Range(CellRefs.First(), CellRefs.Last()).PasteSpecial(Excel.XlPasteType.xlPasteValues, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
            }

            wksht.Cells[1, 1].Select;  <== Error here

            if (sheetProtected)
            { wksht.Protect(); }

            Clipboard.Clear();
            excelApp.ScreenUpdating = true;
        }
    }
Darw1n34
  • 312
  • 7
  • 24

1 Answers1

2

Select() is a method which is used to select a cell or a range of cells and you are trying to use it without function brackets.

try this:

 wksht.Cells[1, 1].Select();
Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79
  • It says I have to wait 8 minutes to do so...trust me i'm on it – Darw1n34 Aug 28 '15 at 16:42
  • any additional thoughts on why the select() class of the range would fail here? – Darw1n34 Aug 28 '15 at 17:05
  • 1
    @Darw1n34 You can check [here](http://stackoverflow.com/questions/21113766/excell-sheets-select-first-row-with-c-sharp) and [here](http://stackoverflow.com/questions/2513968/how-to-focus-a-cell-in-excel-vsto-using-c-how-to-select-first-cell-using-c-sha) – Bhushan Firake Aug 28 '15 at 17:10