JoeBeez
JoeBeez

Reputation: 45

Excel manipulation from C# - Set ActiveCell?

What is the C# equilivant to this VB6 to setting of the active cell?

ActiveSheet.Range("L1").Select

Upvotes: 4

Views: 10743

Answers (1)

Joel Goodwin
Joel Goodwin

Reputation: 5086

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).

Upvotes: 6

Related Questions