gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:57:48 25 4
gpt4 key购买 nike

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

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

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

如果您能帮助找到一种方法来生成一个函数,该函数将 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()”,请在工作表上选择 4 行 x 1 列的区域(返回模块的位置),然后在公式栏中键入“=WriteUnder255Cells()”(不要输入引号)。请注意,这些的调用方式类似于数组公式,因此您不需要点击 (enter) 来创建输出,您需要点击 (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

最佳答案

这对我有用:

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 却没有这样做

关于vba - Excel VBA : Need Workaround for 255 Transpose Character Limit When Returning Variant Array to Selected Range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35395789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com