I'm currently trying to move some VB6 macros into a C# app and I'm having trouble setting the active cell using C#.
In VB6 its simply:
ActiveSheet.Range("L1").Select
Does anyone know what the C# equivalent is?
Cheers in advance.
I'm currently trying to move some VB6 macros into a C# app and I'm having trouble setting the active cell using C#.
In VB6 its simply:
ActiveSheet.Range("L1").Select
Does anyone know what the C# equivalent is?
Cheers in advance.
Here's a sample piece of code:
Excel.Worksheet sht = (Excel.Worksheet)ActiveSheet;
sht.Cells[3, 3] = "HELLO";
You can also capture ranges:
Excel.Range rng = (Excel.Range)sht.Cells[3, 3];
I believe to you just the Select method as before to select a range, although I haven't tested this.
rng.Select();
You can obviously streamline this and chain these statements together, with the right casting. I don't want to hazard a guess here as I've not got a VSTO project open in from of me.
EDIT
You should also be able to get a range from the sheet using get_Range
:
rng = sht.get_Range("A1", Type.Missing);
VSTO tends to return Objects most of the time, necessitating casts, but get_Range
is an exception. Someone might be able to correct me as I am not a big user of VSTO (still VBA die-hard when it comes to Excel).