You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VBA调用EPF.Yahoo.OptionsChain.ExpiryDates函数遇错误2042求解决方案

解决EPF动态数组函数VBA调用错误2042及临时工作表遍历方案

针对错误2042的解决方法

错误2042通常是直接用Evaluate调用动态数组函数时的兼容性问题,试试先在单元格写入动态数组公式,再读取结果:

Dim wsUser As Worksheet
Set wsUser = ActiveWorkbook.ActiveSheet
Dim sgTicker As String
sgTicker = "META"
Dim tempRange As Range

' 找A列最后一行下方的空白单元格写入公式
Set tempRange = wsUser.Cells(wsUser.Rows.Count, 1).End(xlUp).Offset(1, 0)
' 用Formula2写入动态数组公式(旧版Formula不支持动态溢出)
tempRange.Formula2 = "=EPF.Yahoo.OptionsChain.ExpiryDates(""" & sgTicker & """)"

' 读取整个溢出的动态数组区域值到变量a
Dim a As Variant
a = tempRange.SpillingToRange.Value

' 可选:清理临时写入的公式和结果
tempRange.SpillingToRange.ClearContents

退而求其次:创建临时工作表遍历动态数组

如果上面的方法无效,用临时工作表处理:

Dim wsTemp As Worksheet
' 添加临时工作表
Set wsTemp = ActiveWorkbook.Worksheets.Add
Dim sgTicker As String
sgTicker = "META"

' 在A1写入动态数组公式
wsTemp.Range("A1").Formula2 = "=EPF.Yahoo.OptionsChain.ExpiryDates(""" & sgTicker & """)"

' 获取整个溢出的动态数组区域
Dim spillRange As Range
Set spillRange = wsTemp.Range("A1").SpillingToRange

' 遍历区域内的每个单元格
Dim cell As Range
For Each cell In spillRange
    ' 替换成你的处理逻辑,比如打印到立即窗口
    Debug.Print cell.Value
Next cell

' 可选:删除临时工作表,关闭删除提示
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True

关键说明

  • 必须用Formula2而非Formula,动态数组函数依赖Formula2触发溢出行为。
  • SpillingToRange属性可直接获取动态数组溢出的整个区域,无需手动计算行数。

内容的提问来源于stack exchange,提问作者Mikey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 14:29:53