Excel VBA:返回一系列数据的自定义函数

我想在Excel VBA中编写一个可以用作Excel公式的用户定义函数,但不是将值返回给调用它的单元格,而是将值范围返回给其他单元格范围。 这在许多数据库插件中是很常见的,例如Bloomberg,CapIQ,Morningstar。 我尝试过不同的方法,但是找不到一种方法来实现它。

例如,如果我把这个代码放到单元格A1中,它将返回25到单元格A1。

Function AreaOfSquare(length) AreaOfSquare = length * length End Function 

我想要的是这样的:

  ABCDE 1 * Eqt1 Eqt1 Eqt2 Eqt2 2 Price vol Price vol 

在单元格A1中,我把=DBLOOKUP(B1:F1,B2:E2, "One Year") ,并产生这样的结果:

  ABCDE 1 * Eqt1 Eqt1 Eqt2 Eqt2 2 Price vol Price vol 3 07/14 13 100 12 200 4 06/14 14 120 13 210 5 05/14 15 140 14 220 

以下是我尝试的方法列表:

  • 从公式forms的数据库Excel加载项获取原始数据。 例如,我可以键入=Rawdata(B1:E1, B2:E2)并获取原始数据。 问题是我不知道如何在VBA中调用这个函数,以便我可以处理这些数据的输出,因为这个函数不是内置的,而是附带了一个数据库插件。 或者,我可以先使用公式获得原始数据,然后再处理; 问题是,这将需要两步操作,因为我需要键入公式两次。

  • 从Java获取数据并以某种方式导入数据。 这很麻烦,因为我需要在我老板的计算机环境中configurationjava应用程序。

有什么build议么?

这是我如何解决它:首先,find该函数正在调用的单元格。 然后你可以移动它。

在下面的例子中,它将单元格列表的值加起来,直到达到所需的块大小。


 Function bars(bucksize, columnsgap) Application.Volatile 'this function is to count backward number of bars need to fill up the bucket 'bucksize is the volume of the bucket 'columnsgap= how many rows the volume column away from where this function use BB = 0 j = 0 'Find the location where this function call xx = Application.Caller.Column yy = Application.Caller.Row x1 = xx - columnsgap 'Add up the volume of bars until it excess or equal to the bucksize Do rowshift = j y1 = yy - rowshift BB = BB + Cells(y1, x1).Value j = j + 1 Loop While BB < bucksize bars = j End Function