I am working on a UDF(User defined function) for Excel user, the task seems easy with the help of ExcelDNA. But While I test it in a cell of Excel with following two formula, both show #VALUE!. Need help to get this solved, thanks. |
Hi Govert,
Public Module MyFunctionsEnd Module
Public Module MyFunctionsPublic Function AddThem (val1 As Double, val2 As Double) As DoubleReturn val1 + val2End FunctionEnd Module
Public Class MyFunctionsPublic Shared Function AddThem (val1 As Double, val2 As Double) As DoubleReturn val1 + val2End FunctionEnd Class
To post to this group, send email to exc...@googlegroups.com.
To post to this group, send email to exce...@googlegroups.com.
=mySetCellFormula("Test", "A1")=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
ExcelAsyncUtil.QueueAsMacro( _
Sub()
xlCell.FormulaR1C1 = "=" & sFormuaR1C1
End Sub)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); } }
which can be verified by this test function:
[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"; }
I should warn that this approach of mutating different parts of the sheet from a UDF can be pretty dangerous, as it doesn’t integrate with Excel’s functional model and dependency tree at all.
For example, if you are working on another sheet and that causing recalculation of one of these functions, the behaviour from this code might be unexpected.
Anyone trying this should be really careful – it’s not an approach I support or encourage at all.
-Govert
From: exce...@googlegroups.com <exce...@googlegroups.com> On Behalf Of Iron Mask
Sent: 24 March 2019 16:31
To: Excel-DNA <exce...@googlegroups.com>
Subject: [ExcelDna] Re: vb.net - Set Cell Formula in ExcelDNA UDF
Thanks to Govert and Wayne;
--