在VBA(excel)中处理UDF中的长字符串时出现#VALUE错误 [英] #VALUE error when dealing with long string in UDF in VBA(excel)

查看:194
本文介绍了在VBA(excel)中处理UDF中的长字符串时出现#VALUE错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



使用UDF返回长字符串(> 256符号)的数组时,我遇到#VALUE错误。

 函数longString()As Variant 
Dim res(1 To 1,1 To 2)
res(1,1)= hellohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\\\
hellohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\\\
hellohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\\\
hellohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhellohh\\\

RES(1,2)= 世界
longString = RES
端功能


我如何解决这个问题?

解决方案

相信你已经遇到了VBA和Exce之间互动的一个晦涩的限制



一种解决方法是更改​​公式以仅返回单个元素,并将特定元素作为UDF中的参数。



例如:






  Option Explicit 
函数longString(可选R长= 1,可选C长= 1)
Dim res(1到1,1到2)
res(1,1)=hellohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh nhellohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\\\
hellohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\\\
hellohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhellohh\\\

RES(1,2)= 世界
longString = RES(R,C)
端功能






然后您可以通过以下任一方式调用该函数:

  = longString()<  - 返回第一个元素
= longString(1,1)< - 返回第一个元素的Elemen t
= longString(1,2)< - 返回第二个元素
= longString(ROWS($ 1:1),COLUMNS($ A:A))< - 可以拖动并有权返回元素数组


I've encountered #VALUE error when using an UDF returning an array with long strings (>256 symbols).

Sample Code:

Function longString() As Variant
        Dim res(1 To 1, 1 To 2)
        res(1, 1) = "hellohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\nhellohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\nhellohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\nhellohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhellohh\n"
        res(1, 2) = "world"
        longString = res
End Function

When calling longString() as an array formula in a cell, the cell got #Value error, but through debugging, longString() returns without error.

how can i resolve this issue?

解决方案

I believe you have run into one of the obscure limitations in the interactions between VBA and Excel.

One workaround would be to change the formula to return only a single element, and have the particular element as an argument in the UDF.

For example:


Option Explicit
Function longString(Optional R As Long = 1, Optional C As Long = 1)
        Dim res(1 To 1, 1 To 2)
        res(1, 1) = "hellohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\nhellohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\nhellohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\nhellohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhellohh\n"
        res(1, 2) = "world"
        longString = res(R, C)
End Function


You could then call the function in any of the following ways:

=longString()      <-- returns the first element
=longString(1,1)   <-- returns the first element
=longString(1,2)   <-- returns the second element
=longString(ROWS($1:1), COLUMNS($A:A))  <--could be dragged down and right to return an array of the elements

这篇关于在VBA(excel)中处理UDF中的长字符串时出现#VALUE错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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