跨表excel的引用,公式复杂如何提取引用的excel名称并跳转

private Microsoft.Office.Tools.Excel.Button jumpButton;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    AddJumpButton();
}

private void AddJumpButton()
{
    Microsoft.Office.Tools.Excel.Worksheet worksheet = this.Application.ActiveSheet;
    jumpButton = worksheet.Controls.AddButton(50, 50, 100, 30, "jumpButton");
    jumpButton.Text = "Jump to Cell";
    jumpButton.Click += new EventHandler(JumpButton_Click);
}

private void ThisAddIn_Shutdown(object sender, EventArgs e)
{
}

private void JumpButton_Click(object sender, EventArgs e)
{
    JumpToReferencedCell();
}

private void JumpToReferencedCell()
{
    Excel.Worksheet activeSheet = this.Application.ActiveSheet;
    Excel.Range selectedRange = this.Application.Selection as Excel.Range;

    if (selectedRange != null)
    {
        string formula = selectedRange.Formula.ToString();
        var (workbookName, cellAddress) = ExtractReference(formula);
        if (!string.IsNullOrEmpty(workbookName) && !string.IsNullOrEmpty(cellAddress))
        {
            try
            {
                // Ensure the workbook is open
                Excel.Workbook referencedWorkbook = null;
                foreach (Excel.Workbook wb in this.Application.Workbooks)
                {
                    if (wb.Name.Equals(workbookName, StringComparison.OrdinalIgnoreCase))
                    {
                        referencedWorkbook = wb;
                        break;
                    }
                }

                if (referencedWorkbook == null)
                {
                    System.Windows.Forms.MessageBox.Show($"Workbook '{workbookName}' is not open.");
                    return;
                }

                Excel.Range targetRange = referencedWorkbook.Sheets[1].Range[cellAddress]; // Assuming first sheet, modify as needed
                targetRange.Worksheet.Activate();
                targetRange.Select();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Invalid cell reference: " + ex.Message);
            }
        }
        else
        {
            System.Windows.Forms.MessageBox.Show("The selected cell does not contain a valid cross-workbook formula.");
        }
    }
    else
    {
        System.Windows.Forms.MessageBox.Show("No cell is selected.");
    }
}

private (string workbookName, string cellAddress) ExtractReference(string formula)
{
    // Regular expression to match [WorkbookName]SheetName!CellAddress
    var match = System.Text.RegularExpressions.Regex.Match(formula, @"\[(.*?)\].*?!(.*)");
    if (match.Success)
    {
        string workbookName = match.Groups[1].Value;
        string cellAddress = match.Groups[2].Value;
        return (workbookName, cellAddress);
    }
    return (null, null);
}

private void InternalStartup()
{
    this.Startup += new System.EventHandler(ThisAddIn_Startup);
    this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值