Daniel Lenzendorf
Daniel Lenzendorf

Reputation: 107

Get selected excel cell

i am trying to paste something in a specific Excel Cell, but sometimes its paste the data somewhere else.

 workSheet = ExcelApp.Worksheets[2] as Excel._Worksheet;
             Grd_Dep_In.SelectAll();
            Grd_Dep_In.Copy();
            Excel.Range DepIn = cellsRange.get_Range("a2");
            DepIn.Select();
            workSheet.Paste();

Sometimes its pastes in A2, sometimes not.

So i want to check if a2 is the selected cell.

before i start to paste. My Code copys in 8 worksheets and generates pivot tables. and sometimes it mess up and copys somewhere else in the excel worksheet.

So what I need:

if (activeCell= A2)
{
paste

}

But i dont know how to detertimate the active excel cell.

Upvotes: 2

Views: 5369

Answers (1)

Leo Chapiro
Leo Chapiro

Reputation: 13984

Try this:

Excel.Range rng = (Excel.Range) this.Application.ActiveCell;
int row = rng.Row;
int column = rng.Column;

if (row == 1 && column == 1)
{
  // paste
}

Upvotes: 3

Related Questions