如何在 vba 函数中返回 array() 以在单元格中使用它数组公式(矩阵公式):用于在多个单元格中拆分文本 [英] How to return array() in vba function to use it in cells Array formulas (matricial formula) : for split texte in multi cells

查看:41
本文介绍了如何在 vba 函数中返回 array() 以在单元格中使用它数组公式(矩阵公式):用于在多个单元格中拆分文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 VBA 编写一个函数以在 excel 公式中使用,如果我的函数返回单个值也没关系:

I'm writing a function in VBA to use in excel formula, it's ok if my function return a single value:

=MYVALUE(A1)

现在我编写了另一个返回数组(1,2,3,4,...)的函数,并用数组公式替换了我的excel公式:

Now I wrote another function which returns an Array(1,2,3,4,...) and I replace my excel formula by Array formula:

{=MYARRAY(A1)}

但是当我拉伸公式时,所有单元格都显示数组的第一个值.为什么?

But when I stretch the formula, all cells display the first value of my array. why ?

这是我的 VBA 源代码 (complement.xlam):

Here is my VBA source code (complement.xlam) :

Function MYVALUE(x as integer)
    MYVALUE = 123
End Eunction

Function MYARRAY(x as integer)
    MYARRAY = Array(10,20,30)
End Eunction

推荐答案

数组公式需要这样使用

我的 VBA 在多个单元格中拆分文本

my VBA to split text in multi cells

Function EXPLODE_V(texte As String, delimiter As String)
    EXPLODE_V = Application.WorksheetFunction.Transpose(Split(texte, delimiter))
End Function
Function EXPLODE_H(texte As String, delimiter As String)
    EXPLODE_H = Split(texte, delimiter)
End Function

  1. 选择区域 C3:C7 这定义了矢量方向.
  2. F2当场编辑并输入以下公式:=EXPLODE_V($B$3;" ")
  3. CTRL+SHIFT+ENTER(代替通常的ENTER ) - 这将定义一个 ARRAY 公式,并在它周围产生 {=EXPLODE_V($B$3;" ")} 括号(但不要手动输入它们!).

  1. Select region C3:C7 this define the vector direction.
  2. Press F2 to edit on the spot and type the following formula: =EXPLODE_V($B$3;" ")
  3. Press CTRL+SHIFT+ENTER ( INSTEAD of usual ENTER ) - this will define an ARRAY formula and will result in {=EXPLODE_V($B$3;" ")} brackets around it (but do NOT type them manually!).

这篇关于如何在 vba 函数中返回 array() 以在单元格中使用它数组公式(矩阵公式):用于在多个单元格中拆分文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆