Excel VBA:当将Variant数组返回到所选范围时,需要解决方法

我正在努力处理一个涉及明显的Excel 255个字符限制的常见问题。 尝试从函数返回variables数组到工作表上的选定区域时遇到错误。 当函数的返回数组中的每个单元格都小于255个字符时,它们会按照它们应该的方式发布到工作表:在选定范围内的每个单元格中会出现一个元素。 但是,如果返回的variables数组中的任何元素长于255个字符,我会得到一个值! 错误。 这些错误是不好的,因为我需要我的长元素,并希望保持数据在一起!

这个问题的版本在许多论坛上反复出现,但是当数组单元格超过255个字符时,我可以find一个简单的通用解决scheme,用于将variables数组返回到所选范围(不一定包含公式)。 我最大的元素大约是1000,但如果解决scheme可以容纳多达2000个字符的元素,那将会更好。

最好,我希望这是用一个函数,或可以添加到我的function(不是子例程)的附加代码行。 我想避免子程序的原因是:我不想对任何范围进行硬编码。 我希望这是灵活的,输出位置是dynamic的基于我当前的select。

请,如果你能帮助find一种方法来产生一个函数,它采用一个Variant数组作为input,并维护所需的数组:单元格1:1的关系,我会很感激。

所以这个function与短细胞工作:

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 

但是这个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 

这是如何产生输出和结果:

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

这里有一些文件/论坛的讨论。 这些解决scheme似乎是过于具体或笨拙,因为它们引起子程序(我想避免):

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:使用长度超过255个字符的公式

数组值超过255个字符时出现VBA代码错误

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

超过255个字符的数组公式

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 

看起来像你需要返回一个string数组和Application.Transpose不这样做