Excel VBA:在将变体数组返回到选定范围时需要255个转置字符限制的解决方法

时间:2016-02-14 18:56:16

标签: excel vba function excel-vba character-limit

我正在努力解决一个涉及明显的Excel 255字符限制的常见问题。尝试将变量数组从函数返回到工作表上的选定范围时遇到错误。 当Function的返回数组中的每个单元格都不超过255个字符时,它们就像它们应该的那样发布到工作表:在所选范围内的每个单元格中出现一个元素。但是,如果我返回的变量数组中的任何元素超过255个字符,我会得到一个值!错误。这些错误很糟糕,因为我需要我的长元素,并希望将数据保持在一起!

这个问题的版本在很多论坛中反复出现,但我能够找到一个清晰简单,通用的解决方案,用于在数组中将变量数组返回到所选范围(不一定包含公式)单元格超过255个字符。我最大的元素大约是1000,但如果解决方案可以容纳最多2000个字符的元素,那就更好了。

最好,我希望用函数或其他代码行来实现,这些代码可以添加到我的函数(不是子程序)。我想避免子程序的原因是:我不想对任何范围进行硬编码。我希望这是灵活的,输出位置是根据我当前的选择动态的。

请,如果你能帮助找到一种方法来生成一个函数,它将Variant数组作为输入,并保持所需的数组:cell 1:1关系,我会非常感激。

所以这个短细胞的功能起作用:

Function WriteUnder255Cells()

Dim myArray(3) As Variant  'this the variant array I will attempt to write

' Here I fill each element with less than 255 characters
' it should output them if you call the function properly.
myArray(0) = "dog"
myArray(1) = "cat"
myArray(2) = "bird"
myArray(3) = "fly"

WriteUnder255Cells = Application.Transpose(myArray())

End Function

但是这个功能超过255的功能将不会输出。

Function WriteOver255Cells()

Dim myArray(3) As Variant  'this the variant array I will attempt to write

' Here I fill each element with more than 255 characters
' exceeding the 255-character limit causes the VALUE! errors when you output them
myArray(0) = "ThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelaxydog"
myArray(1) = "ThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydog"
myArray(2) = "ThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydog"
myArray(3) = "ThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydog"

WriteOver255Cells = Application.Transpose(myArray())

End Function

这是您生成输出和结果的方法:

首先,您需要创建两个模块(将一个函数插入每个模块,将代码从一个模块粘贴到相应的模块中)。要运行“WriteUnder255Cells()”,请在工作表上选择一个4行x 1列的区域(这是您返回模块的位置)并在公式栏中键入“= WriteUnder255Cells()”(不要输入引号)。注意这些被称为数组公式,因此不需要按(输入)来创建输出,而是你需要点击(control + shift + enter)。对WriteOver255Cells()重复相同的过程以产生错误。

以下是一些解决它的文件/论坛讨论。解决方案似乎过于具体或笨重,因为它们唤起子程序(我想避免):

https://support.microsoft.com/en-us/kb/213841

http://www.mrexcel.com/forum/excel-questions/852781-visual-basic-applications-evaluate-method-255-character-limit.html

Excel: Use formula longer that 255 characters

VBA code error when array value exceeds 255 characters

http://dailydoseofexcel.com/archives/2005/01/10/entering-long-array-formulas-in-vba/

https://forums.techguy.org/threads/solved-vba-access-to-excel-255-char-limit-issue.996495/

http://www.mrexcel.com/forum/excel-questions/494675-255-character-cell-limit-visual-basic-applications-workaround.html

Array formula with more than 255 characters

http://www.mrexcel.com/forum/excel-questions/388250-size-limit-transferring-variant-range-excel-2007-a.html

1 个答案:

答案 0 :(得分:4)

这对我有用:

Function Over255()

    Dim myArray(3)  As String '<<<<< not variant

    myArray(0) = String(300, "a")
    myArray(1) = String(300, "b")
    myArray(2) = String(300, "c")
    myArray(3) = String(300, "d")

    'Over255 = Application.Transpose(myArray())
    Over255 = TR(myArray)

End Function

'like Application.Transpose...
Function TR(arrIn) As String()
    Dim arrOut() As String, r As Long, ln As Long, i As Long

    ln = (UBound(arrIn) - LBound(arrIn)) + 1
    ReDim arrOut(1 To ln, 1 To 1)
    i = 1
    For r = LBound(arrIn) To UBound(arrIn)
        arrOut(i, 1) = arrIn(r)
        i = i + 1
    Next r
    TR = arrOut

End Function

好像你需要返回一个字符串数组而Application.Transpose不这样做