Return vba value from xlwings

Use this example in the xlwings documentation .

Given the following python code:

import numpy as np
from xlwings import Workbook, Range

def rand_numbers():
    """ produces std. normally distributed random numbers with shape (n,n)"""
    wb = Workbook.caller()  # Creates a reference to the calling Excel file
    n = int(Range('Sheet1', 'B1').value)  # Write desired dimensions into Cell B1
    rand_num = np.random.randn(n, n)
    Range('Sheet1', 'C3').value = rand_num

      

This is an original example.

Let's say we change it a bit:

import numpy as np
from xlwings import Workbook, Range

def rand_numbers():
    """ produces std. normally distributed random numbers with shape (n,n)"""
    wb = Workbook.caller()  # Creates a reference to the calling Excel file
    n = int(Range('Sheet1', 'B1').value)  # Write desired dimensions into Cell B1
    rand_num = np.random.randn(n, n)
    return rand_num #modified line

      

And we call it from VBA using the following call:

Sub MyMacro()
    dim z 'new line'
    z = RunPython ("import mymodule; mymodule.rand_numbers()")
End Sub

      

We get it z

as empty.

Is there a way to return the vba value directly without writing to a text file, or to put the value first into an excel document?

Thanks for any pointers.

+3


source to share





All Articles