在ExcelDNA UDF中设置单元格公式

时间:2016-01-27 02:18:39

标签: vb.net udf excel-dna

我正在为Excel用户开发UDF(用户定义函数),在ExcelDNA的帮助下,任务似乎很容易。但是当我在Excel的单元格中使用以下两个公式测试它时,都显示#VALUE!。需要帮助才能解决这个问题,谢谢。

=mySetCellFormula("Test", "")
=mySetCellFormula("Test", "A1")

Imports System.Net
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.Office.Core
Imports Excel = Microsoft.Office.Interop.Excel
Imports ExcelDna.Integration
Imports ExcelDna.Integration.XlCall

Public Module MyFunctions
    Public Function mySetCellFormula(ByVal sFormuaR1C1 As String, ByVal cellAddress As String) As String
        ' Get the correct application instance
        Dim xlApp As Excel.Application
        Dim xlSheet As Excel.Worksheet
        Dim xlCell As Excel.Range
        xlApp = CType(ExcelDnaUtil.Application, Excel.Application)
        xlSheet = CType(xlApp.ActiveSheet(), Excel.Worksheet)
        If cellAddress = "" Then
            xlCell = xlApp.ActiveCell()
        Else
            xlCell = xlSheet.Range(cellAddress)
        End If
        'xlCell.FormulaR1C1 = "=" & Chr(34) & sFormuaR1C1 & Chr(34)
        xlCell.FormulaR1C1 = "=" & sFormuaR1C1
        mySetCellFormula = ""
    End Function
End Module

2 个答案:

答案 0 :(得分:1)

感谢Govert在 Excel计算模型概念上启发我,最后我找到了一个解决方案如下:

Imports System.Net
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.Office.Core
Imports Excel = Microsoft.Office.Interop.Excel
Imports ExcelDna.Integration
Imports ExcelDna.Integration.XlCall

Public Module MyFunctions
    Public Function mySetCellFormula(ByVal sFormuaR1C1 As String, ByVal cellAddress As String) As String
        ' Get the correct application instance
        Dim xlApp As Excel.Application
        Dim xlSheet As Excel.Worksheet
        Dim xlCell As Excel.Range
        xlApp = CType(ExcelDnaUtil.Application, Excel.Application)
        xlSheet = CType(xlApp.ActiveSheet(), Excel.Worksheet)
        If cellAddress = "" Then
            xlCell = xlApp.ActiveCell()
        Else
            xlCell = xlSheet.Range(cellAddress)
        End If

        ExcelAsyncUtil.QueueAsMacro( _
            Sub()
                xlCell.FormulaR1C1 = "=" & sFormuaR1C1
            End Sub)

        mySetCellFormula = ""
    End Function
End Module

答案 1 :(得分:0)

感谢Govert和Wayne;我需要相同的实现,但在C#中: 我以对我有用的以下内容结束了:

public static void SetCellFormula(string cellAddress, string sFormua)
{
  try
  {
    Excel.Range xlCell;
    var xlApp = (Excel.Application)ExcelDnaUtil.Application;
    var activeSheet = xlApp.ActiveSheet;
    var xlSheet = (Excel.Worksheet)activeSheet;
    if ( string.IsNullOrEmpty(cellAddress))
    {
      xlCell = xlApp.ActiveCell;
    }
    else
    {
      xlCell = xlSheet.get_Range(cellAddress);
    }

    ExcelAsyncUtil.QueueAsMacro(
      () =>
        {
          xlCell.Formula = $"={sFormua}";
        });
  }
  catch (Exception ex)
  {
    Debug.WriteLine(ex);
  }
}

以及;

 public static void SetCellValue(string cellAddress, object cellValue)
{
  try
  {
    Excel.Range xlCell;
    var xlApp = (Excel.Application)ExcelDnaUtil.Application;
    var activeSheet = xlApp.ActiveSheet;
    var xlSheet = (Excel.Worksheet)activeSheet;
    if (string.IsNullOrEmpty(cellAddress))
    {
      xlCell = xlApp.ActiveCell;
    }
    else
    {
      xlCell = xlSheet.get_Range(cellAddress);
    }

    ExcelAsyncUtil.QueueAsMacro(
      () =>
        {
          xlCell.Value2 = cellValue;
        });
  }
  catch (Exception exc)
  {
    Debug.WriteLine(exc);
  }
}

可以通过以下方式验证:

  [ExcelFunction(Name = "TestFormula", Description = "Test Set Formula on Cell")]
public static string TestFormula()
{
  SetMyCellValue("A1", 1);

  SetMyCellValue("A2", 2);
  SetMyCellValue("A3", 4);
  SetMyCellValue("A4", 8);

  SetMyCellValue("B1", -1);
  SetMyCellValue("C1", 3);
  SetMyCellValue("D1", 5);

  SetCellFormula("B2", "$A$1*B$1-$A2");

  return "=TestFormula";
}