How to focus a cell in Excel VSTO using C# or to select first cell using C# in VSTO?
To switch focus in Excel, simply press the F2 key on your keyboard. This will instantly move the focus to the cell or range that you have selected. You can then use the arrow keys on your keyboard to move around within that cell or range.
Use the Active Cell PropertyType the keyword “ActiveCell”. Type a dot (.) to get the list properties and methods. Select the property or method that you want to use. Run the code to perform the activity to the active cell.
To select a range of cells without dragging the mouse: Click in the cell which is to be one corner of the range of cells. Move the mouse to the opposite corner of the range of cells. Hold down the Shift key and click.
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.
To select the A2 cell, for example:
Excel.Worksheet Worksheet = excel.ActiveWorkbook.ActiveSheet;
Worksheet.get_Range([1,2], System.Reflection.Missing.Value).Select();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With