- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用的是 Excel 2010。
当 VBA 用户定义函数(例如 make2DArray)输出表示 2D 数组的变体时,该 2D 数组可以用作另一个 VBA 函数(例如 someFunction)的输入。从 VBA 代码中调用函数时。效果很好。
但是,当执行相同操作时,从工作表内(通过单元格中的公式)将 make2DArray 输出作为参数传递给 someFunction,那么它似乎仅适用于具有 **2 行或更多行** 的 2D 数组。生成 1 行的 2D 数组将会失败。似乎 1 行 2D 数组会自动转换为等效的 1D 数组。
一个小例子:
Option Explicit
'returns last (top-most, right-most) element
Function someFunction(v As Variant) As Integer
On Error Resume Next
Debug.Print "Dim-1 size", UBound(v, 1) - LBound(v, 1) + 1
Debug.Print "Dim-2 size", UBound(v, 2) - LBound(v, 2) + 1
On Error GoTo 0
someFunction = v(UBound(v, 1), UBound(v, 2))
End Function
Function make2DArray(h As Integer, w As Integer) As Variant
Dim i As Integer, j As Integer
Dim v2d As Variant
ReDim v2d(1 To h, 1 To w)
For i = 1 To h
For j = 1 To w
v2d(i, j) = i * j
Next j
Next i
make2DArray = v2d
End Function
Sub test()
'also works when called from sheet
Debug.Print someFunction(make2DArray(2, 3)) 'returns 6
'doesn't work when called from sheet
Debug.Print someFunction(make2DArray(1, 3)) 'returns 3
End Sub
测试函数在 VBA 中可以正常工作。同样, =someFunction(make2DArray(2, 3)) 或任何 =someFunction(make2DArray(i, j)) 单元格公式对于 i>1 都适用,但是 =someFunction(make2DArray(1, 3)) 或任何 =someFunction(make2DArray) (1, j)) 只会产生#VALUE!结果在工作表中。
我的问题:这种行为是否记录在某处?有没有办法避免 1 行 2D 数组从 2D 数组“转换”到 1D 数组?
最佳答案
来 self 的评论:
I've never seen that before. It is like it is using transpose to convert a 2-D (one row) array into a 1-D array. I consider this a bug in that the VBA overhead is looking at a 2-D variant array with one 'row' (e.g. 1 as ubound first rank) and converting it to a 1-D array with the original ubound second rank as the ubound of the sole 1-D rank. For universality, the only suggestion I can come up with would be error control; I'll post a suggestion below.
这里有一些错误控制来克服恶意的 2-D 到 1-D 数组转换。
'returns bottom-right element of 2D-array
Function someFunction(v As Variant) 'As Integer
On Error Resume Next
Debug.Print "Dim-1 size", UBound(v, 1) - LBound(v, 1) + 1
Debug.Print "Dim-2 size", UBound(v, 2) - LBound(v, 2) + 1
On Error GoTo 0
'Debug.Print IsArray(v)
'Debug.Print UBound(v, 1) & ":" & UBound(v, 2)
On Error GoTo err_1D_array
someFunction = v(UBound(v, 1), UBound(v, 2))
Exit Function
err_1D_array:
someFunction = v(UBound(v))
End Function
关于excel - 将 2D 数组从工作表传递到 VBA/UDF 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47706441/
我在弄清楚如何从另一个 UDF 返回 UDF 中的数组时遇到了一些麻烦。这里的一个是简单的指数移动平均 UDF,我试图将数组返回到另一个 UDF,但我收到 #value 错误。我觉得有一个我没有看到的
我需要从另一个模块(在同一个工作簿中)调用以前制作的 UDF 来构建另一个 UDF。如何调用第一个函数? 这适用于 Excel VBA。我已经构建了我的第一个函数,它计算产品在特定时间的账面值(val
这个问题可能对许多 VBA 程序员有用。它涉及实现两个有用的独立任务并使它们同时工作。 第一个任务是为 UDF 制作 Excel 函数工具提示。虽然似乎还没有找到明确的解决方案,但目前我对自定义插入函
我正在将 Spark 与 Scala 一起使用,并希望将整行传递给 udf 并选择 udf 中的每个列名和列值。我怎样才能做到这一点? 我正在尝试以下 - inputDataDF.withColumn
这个问题在这里已经有了答案: Spark functions vs UDF performance? (3 个答案) 关闭2 年前。 我从 Pyspark 网站获取了以下 UDF,因为我试图了解是否
我已经使用 Spark 2.4 一段时间了,最近几天才开始切换到 Spark 3.0。切换到 Spark 3.0 运行后出现此错误 udf((x: Int) => x, IntegerType)
这个问题源自 SQLServer: Why avoid Table-Valued User Defined Functions? 。我开始在一些评论中提出问题,而对我评论的回复却偏离了主题。 这样您就
这是我的 hive 表 CREATE TABLE `dum`(`val` map>); insert into dum select map('A',array('1','2','3'),'B',ar
我想知道编写 spark udf 是否会降低性能。一般来说,我更喜欢组合做一件事的小函数…… 这是一个简单的例子,给定一个 DataFrame df: def inc = udf( (i: Doubl
我正在尝试根据另一列的值在 Spark 数据集中创建一个新列。另一列的值作为键在 json 文件中搜索,返回的值是用于新列的值。 这是我尝试过的代码,但它不起作用,而且我不确定 UDF 是如何工作的。
SPARK_VERSION = 2.2.0 我在尝试做 filter 时遇到了一个有趣的问题。在具有使用 UDF 添加的列的数据帧上。我能够用较小的数据集复制问题。 鉴于虚拟案例类: case cla
我正在 Java 中使用 Spark 来处理 XML 文件。来自databricks的spark-xml包用于将xml文件读入dataframe。 示例 xml 文件是: 1 joh
我正在尝试创建一个 MySQL UDF getFile(),它应该从磁盘上的某个目录返回文本文件的内容。问题是调用一次或两次有效,但在第二次或第三次调用 UDF 时,MySQL 服务器崩溃。 我无法通
我听说 Microsoft SQL Server 中有多种方法可以查找“最差”存储过程:按执行次数、按 CPU 工作时间、按队列等待时间等。 我正在寻找一种方法来查找最差(最慢/最常用)的 UDF -
我已经为一个项目构建了一个包含多个公式的 Excel 工作表。然后,我添加了一个用于折叠/展开某些单元组的命令按钮。 命令按钮代码是: Private Sub CommandButton1_Click
MySQL版本:5.1.73数据库客户端版本:libmysql - 5.1.73 我试图检查 NEW.src 在过去一小时内是否存在,如果不存在则执行 sys_exec udf。 我在 mysql 中
我正在尝试将元组列表传递给 scala 中的 udf。我不确定如何为此准确定义数据类型。我试图将它作为一整行传递,但它无法真正解决它。我需要根据元组的第一个元素对列表进行排序,然后返回 n 个元素。我
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 此问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-topic
我正在尝试创建一个类似 =Extractinfo("A2","Name") 的函数,它可以从原始数据中提取姓名、电话和电子邮件 ID,一个用于所有 3 次提取的函数,我已经有一个提取电子邮件 ID 的
我正在编写一个用户定义函数(UDF),它以一些单元格作为参数。 这些单元格包含相同的数据,但精度不同;该功能显示可用的最佳精度。 函数的参数按精度升序编写。 这是一个例子: +---+--------
我是一名优秀的程序员,十分优秀!