15

How to focus a cell in Excel VSTO using C# or to select first cell using C# in VSTO?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
venkat
  • 5,648
  • 16
  • 58
  • 83

3 Answers3

29

Here is one way:

Excel.Worksheet activeSheet = ThisAddIn.ExcelApplication.ActiveSheet;
var range = activeSheet.get_Range("A1", "A1");
range.Select();

ThisAddIn is the name of my test project.

Mikael Svenson
  • 39,181
  • 7
  • 73
  • 79
  • 1
    You can also add a reference to Microsoft.Office.Interop.Excel.Extensions; so that you can do var range = activeSheet.Range("A1"); in place of the 2nd line. – Anonymous Type Sep 28 '10 at 00:22
  • 8
    An important thing to note here is that you can only call Select on the active sheet. So if you need to select a cell in something other than the currently active sheet, you'll need to call Activate on it first. – Phred Menyhert Mar 04 '12 at 22:09
3

To select the A2 cell, for example:

Excel.Worksheet Worksheet = excel.ActiveWorkbook.ActiveSheet;
Worksheet.get_Range([1,2], System.Reflection.Missing.Value).Select();
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
praveena
  • 31
  • 1
3
 Excel.Application ExcelApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
            Excel.Workbook book = ExcelApp.ActiveWorkbook;
            Excel.Worksheet sheet = book.ActiveSheet;
            Excel.Range ExcelRange = sheet.get_Range("A1");
            ExcelRange.Select();
KR Akhil
  • 877
  • 3
  • 15
  • 32
  • You realize that was 7 years ago? What does your answer add that is not already in the proposed answers? Explain your solution at least. – Nic3500 Dec 06 '17 at 07:05