开发者

Pushing 2-d Variant data into FormulaArray

开发者 https://www.devze.com 2023-04-13 01:27 出处:网络
I originally had a VBA function that returned a two dimensional variant array: Public Function SplitIntoCells(some_data As String) As Variant()

I originally had a VBA function that returned a two dimensional variant array:

Public Function SplitIntoCells(some_data As String) As Variant()
End Function

I used the Formula array syntax to call it from another vba function:

Public Function MyWrapper() as Variant
    MyWrapper = SplitIntoCells("somestring")
End Function

From Excel, if I select a big enough range and then do:

=MyWrapper()

followed by CTRL+SHIFT+ENTER, the data is nicely split into every individual cell in that range.

However in order to automate that, if I change MyWrapper to:

Public Function MyWrapper()
    ActiveSheet.Range("A1:E20").Select
    Selection.FormulaArray = SplitIntoCells("somestring")
End Function

The above doesn't work. I see nothing being displayed in Excel.

What am I doing wrong?

Update:

Just for testing, if I slightly modify MyWrapper() to:

Public Function MyWrapper()
    Dim variant_temp() as Variant
    variant_temp = SplitIntoCells("somestring")
    ActiveSheet.Range("A1:E20").Select
    Selection.FormulaArray = variant_temp
End Functio开发者_C百科n

variant_temp predictably has the 2-d array after returning from SplitIntoCells but the subsequent Selection.FormulaArray still has nothing in it even after the assignment. I am sure I am missing something blindingly obvious.


A VBA user-defined function cannot modify the cell or range it's being called from: it can only return a value.

0

精彩评论

暂无评论...
验证码 换一张
取 消