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

Excel VBA: Need Workaround for 255 Transpose Character Limit When Returning Variant Array to Selected Range

我正在努力解决一个明显的 Excel 255 个字符限制的常见问题。我在尝试 return 从函数到 select 工作范围 sheet 的变体数组时遇到错误。 当函数的 returning 数组中的每个单元格都少于 255 个字符时,它们 post 到 sheet 就像它们应该的那样:一个元素出现在每个单元格中selected 范围。但是,如果我的 returning 变体数组中的任何元素超过 255 个字符,我就会得到一个值! error. 这些错误很糟糕,因为我需要我的 long 元素并希望将数据保持在一起!

这个问题的版本在许多论坛中一遍又一遍地出现,但我能够找到一个清晰简单的通用解决方案,用于 returning 变体数组到 select 当数组单元格超过 255 个字符时,ed 范围(不一定包含公式)。 我最大的元素是 1000 左右,但如果解决方案可以容纳最多 2000 个字符的元素会更好。

最好,我希望用一个函数或可以添加到我的函数中的附加代码行来实现它(不是子例程)。我不想使用子程序的原因是:我不想对任何范围进行硬编码。我希望这是灵活的,并且输出位置可以根据我当前的 selection 动态变化。

拜托,如果你能帮助找到一种方法来生成一个函数,该函数将 Variant Array 作为输入,并保持所需的 array: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()", select sheet 上的 4 行 x 1 列的区域(这是你 return 模块的地方)并键入“=WriteUnder255Cells( )”到公式栏中(不要输入引号)。请注意,这些被称为数组公式,因此不是点击 (enter) 来创建输出, 你需要点击 (control + shift + enter)。对 WriteOver255Cells() 重复相同的过程以产生错误。

这里有一些 documents/forums 讨论解决了这个问题。这些解决方案似乎过于具体或笨拙,因为它们会引发子程序(我想避免):

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

这对我有用:

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

您似乎需要 return 一个字符串数组,而 Application.Transpose 不需要