facebooktwittermenuarrow-up

GemBox Support Forum

Hyperlink across worksheets

How can I set a HyperLink to a specific cell in another worksheet in my current workbook?
I found this article: Excel cell hyperlinks.

This is my current code which does not work:

var workbook = ExcelFile.Load(sFile);
var worksheets = workbook.Worksheets;

//worksheets.ActiveWorksheet = worksheets["REV2-Sub Menu"];
ExcelWorksheet ws = workbook.Worksheets["REV2-Start OSE-MainMenu"];

var hyperlinkStyle = workbook.Styles[BuiltInCellStyleName.Hyperlink];
var cell = ws.Cells[35, 5];
cell.Value = "jump";
cell.Style = hyperlinkStyle;
cell.Hyperlink.Location = workbook.Worksheets["REV2-Sub Menu"] + "!A4";

Hi Herb,

You need to use single quotes around the sheet’s name.

In other words, try this:

cell.Hyperlink.Location = "'REV2-Sub Menu'!A4";

Or this:

var menuSheet = workbook.Worksheets["REV2-Sub Menu"];
cell.Hyperlink.Location = $"'{menuSheet.Name}'!A4";

Does this solve your issue?

Regards,
Mario

Impressive. Thank you.

This is my current code:

var hyperlinkStyle = workbook.Styles[BuiltInCellStyleName.Hyperlink];
var cell = ws.Cells[26, 2];
cell.Value = jump;
cell.Style = hyperlinkStyle;
string sSheet = REV2-Sub Menu;
cell.Hyperlink.Location = $'{sSheet}'!A4; //WORKS!!!!!!!!!!!

Can I in line 2 var cell = ws.Cells[26, 2];, specify a specific worksheet other than the active one? Or must I specify the worksheet prior to var cell = ws.Cells[26, 2] each time I need to set the cell value? Kind of like what I’m doing in line 6 cell.Hyperlink.Location = $'{sSheet}'!A4;.

Try this:

var cell = workbook.Worksheets["Sheet Name"].Cells[26, 2];

// Or

int sheetIndex = 0;
var cell = workbook.Worksheets[sheetIndex].Cells[26, 2];

In your " Hyperlink, update real-time - GemBox.Spreadsheet - GemBox Support Forum (gemboxsoftware.com)" You gave me a solution for:
cell.Hyperlink.Location = CellRange.RowColumnToPosition(objectRow, objectColumn);
I provided an incomplete request in this thread.
In the “Hyperlink.Location” I need to provide the worksheet and the row and column.

I tried: cell.Hyperlink.Location = $"’{sWSheet}’" + “CellRange.RowColumnToPosition(isControl, 4)”;
But that is incorrect.
How do I combined specifying the worksheet and specifying the row/column for the hyperlink?

The code I’m building is using variable for everything:
worksheet
row
column

Is sWSheet an instance of an ExcelWorksheet?

If yes, then use this:

cell.Hyperlink.Location = $"'{sWSheet.Name}'!{CellRange.RowColumnToPosition(isControl, 4)}";