adh*_*dhg 0 excel vba function excel-vba subroutine
我想编写一个函数,其结果应该在一系列单元格中打印/写入数组.
活动单元应该是第一个元素,下面的下一个单元格是第二个元素(依此类推).因此,例如,如果我当前的活动单元格是B2,则所需结果应如下图所示.
我的代码仅适用于Debug.Pring,但我无法弄清楚如何在excel表上实际使用它.
Function ShowResult()
Dim strArray() As String
Dim result As String
result = "Maybe I think too much but something's wrong"
strArray = Split(result, " ")
Dim StartRow, i As Integer
StartRow = 1
For i = 0 To UBound(strArray)
Debug.Print strArray(i)
'Range("A" & i + StartRow).Value = strArray(i) <--I tried even with this, didn't work!
Next
End Function
Run Code Online (Sandbox Code Playgroud)
稍微改变你的功能:
Function ShowResult() As Variant
Dim strArray() As String
Dim result As String
result = "Maybe I think too much but something's wrong"
strArray = Split(result, " ")
For i = 0 To UBound(strArray)
Debug.Print strArray(i)
'Range("A" & i + StartRow).Value = strArray(i) <--I tried even with this, didn't work!
Next
ShowResult = Application.Transpose(strArray)
End Function
Run Code Online (Sandbox Code Playgroud)
然后在使用时你需要选择足够的单元格来覆盖整个字符串:
然后输入您的公式:
=ShowResults()
Run Code Online (Sandbox Code Playgroud)
按Ctrl-Shift-Enter键使其成为数组公式:
如果正确完成excel将{}围绕公式.