使用Excel内置函数的UDF顶部 [英] Using Excel Built-in Functions on top of the UDF

查看:324
本文介绍了使用Excel内置函数的UDF顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么,出于某种原因,Excel内置函数 = SUM()不能真正总结通过自定义UDF产生的结果。它似乎总是落得0。

例如,我有一个下面的UDF,名为 myUDF ,将返回的项目数。

有关细胞 A1
公式: = myUDF(意达)结果:1​​0

有关细胞 B1
公式: = myUDF('ItemB')结果:1​​5

所以当我做 = SUM(A1:B1),把公式单元格 C1 ,它赢得T返回25,但0代替。

我曾尝试用一些数据格式化的东西(转换为数字),但仍没有运气。有没有人在这里也有类似的问题过吗?在它的原因的任何想法?

感谢。

编辑: code样品

 公共对象MyUDF(串号,串PK,字符串参数1 =,字符串参数2 =,字符串参数3 =,串param4 =)
        {
            对象result = NULL;
            串strFormula;

            双N = 0;
            DateTime的DT;

            尝试
            {
                strFormula = buildFormula(ID,PK,参数1,参数2,参数3,param4);

                结果= ws.getServiceResultsDataString(ID,objUser,PK,参数1,参数2,参数3,param4);

                如果(double.TryParse(result.ToString(),n个))
                {
                    结果= N;
                }
                否则,如果(DateTime.TryParse(result.ToString(),出DT))
                {
                    结果= dt.Date;
                }
                ws.Dispose();
                objUser = NULL;

            }
            赶上(例外前)
            {
            }
            最后
            {
            }
            返回结果;
        }
 

解决方案

是您的UDF返回的的或的字符串恰好包含数字字符的?

如果我去到Excel和'10型(10,格式化为一个字符串)和'15(15,格式化字符串),和SUM()他们,我得到0。

如果我输入10和15(格式为数字)和SUM()他们,我得到25。

一些例如code能重现问题,使我们能够肯定的回答。

I don't know why for some reason the Excel built-in function =SUM() can't really sum up the results created by the custom UDF. It appears to always end up with 0.

For example, I have got a following UDF, called myUDF, which will return the number of items.

For the cell A1:
Formula: =myUDF('ItemA') Result: 10

For the cell B1:
Formula: =myUDF('ItemB') Result: 15

So When I do =Sum(A1:B1) and put the formula in cell C1, it won't return 25 but 0 instead.

I have tried to use some data formatting stuff (converting to numeric) but still no luck there. Has anyone here had similar issue before? Any ideas on the cause of it?

Thanks.

EDIT: Code Sample

        public object MyUDF(string id, string pk, string param1 = "", string param2 = "", string param3 = "", string param4 = "")
        {
            object result = null;
            string strFormula;

            double n = 0;
            DateTime dt;

            try
            {
                strFormula = buildFormula(id, pk, param1, param2, param3, param4);

                result = ws.getServiceResultsDataString(id, objUser, pk, param1, param2, param3, param4);

                if (double.TryParse(result.ToString(), out n)) 
                {
                    result = n;
                }
                else if (DateTime.TryParse(result.ToString(), out dt))
                {
                    result = dt.Date;
                }
                ws.Dispose();
                objUser = null;

            }
            catch (Exception ex)
            {               
            }
            finally
            {   
            }
            return result;
        }

解决方案

Is your UDF returning a number or a string that happens to contain numeric characters?

If I go into Excel and type in '10 (10, formatted as a string) and '15 (15, formatted as a string), and SUM() them, I get 0.

If I type 10 and 15 (formatted as numbers) and SUM() them, I get 25.

Some example code that reproduces the problem would allow us to answer for sure.

这篇关于使用Excel内置函数的UDF顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆