facebooktwittermenuarrow-up

GemBox Support Forum

How to set current cell as active?

I have a follow-up to my previous question after I select some sheet as active, how can I select a cell within that sheet as active?

I want to avoid “forcing” someone to scroll down to the last empty row when he opens the file in Excel.
In other words, I need something like “scroll into a cell” functionality.

Hi hertzogth,

You can set what the first visible cell will be when the file is opened using the FirstVisibleRow and FirstVisibleColumn properties.

For example:

var workbook = ExcelFile.Load("Book.xlsx");
var worksheet = workbook.Worksheets.ActiveWorksheet;

// Set last row which has some data as first visible row.
int lastRowIndex = worksheet.Rows.Count - 1;
worksheet.ViewOptions.FirstVisibleRow = lastRowIndex;

workbook.Save("Book.xlsx");

But note that this only affects the view, the selected cell (or cells) will remain the same.
If you need that as well, then you can also add the following:

// Set first cell in first empty row as selected cell.
var lastCell = worksheet.Rows[lastRowIndex + 1].Cells[0];
worksheet.SelectedRanges.Add(worksheet.Cells.GetSubrange(lastCell.Name));

Does this solve your issue?

Regards,
Mario