gpt4 book ai didi

arrays - Excel VBA 中的数组函数

转载 作者:行者123 更新时间:2023-12-01 21:47:49 61 4
gpt4 key购买 nike

我在 VBA 中有一个函数可以生成字符串数组。从另一个 VBA 函数调用时它工作正常,但从工作表调用时则不然。

使用方法如下:

  • 选择A1:A3
  • 在公式栏中写入=Test(),然后按Ctrl-Shift-Enter使其成为数组函数
  • A1 应包含 A,A2 应包含 B,A3 应包含 C

当我实际尝试时,它会将 A 放入数组的所有三个单元格中。如何将 Test 返回的数据放入数组的不同单元格?

<小时/>

对于那些想查看它的人,这里是该函数的代码。请记住,当从其他函数调用时,该函数可以正常工作。

Function Test() As String()
Dim a(1 To 3) As String
a(1) = "A"
a(2) = "B"
a(3) = "C"
Test = a
End Function

最佳答案

如果你转置数据,你的函数就可以正常工作。为此,您需要 Variant() 而不是 String():

Function Test() As Variant()
Dim a(1 To 3) As Variant
a(1) = "A"
a(2) = "B"
a(3) = "C"
Test = Application.Transpose(a)
End Function

关于arrays - Excel VBA 中的数组函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10290591/

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