excel - 未定义子或函数 System.Reflection.TargetInvocationException
问题描述
我目前正在使用自动运行 excel 宏的应用程序。测试时我收到这个:
运行宏 RefreshAllPMWS 时出错,组件名称:模块 1 === 错误消息:未定义子或函数 组件名称:模块2 === 错误消息:未定义子或函数 System.Reflection.TargetInvocationException:异常已由目标引发调用。---> System.IndexOutOfRangeException:来自 HRESULT 的异常:0x800A0009 --- 内部异常堆栈跟踪结束 --- 在 System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers , Int32 区域性, String[] namedParameters) 在 System.RuntimeType.InvokeMember(字符串名称, BindingFlags bindingFlags, Binder binder, Object 目标, Object[] providedArgs, ParameterModifier[] 修饰符, CultureInfo 区域性, String[] namedParams)
以下是模块 1 中的代码:
Option Explicit
Public Sub Month_End()
' Month_End Macro
' Updates Formulas for YTD ME to include the most recent day's PnL
'
Range("D15").Select
ActiveCell.FormulaR1C1 = "=+SUM(RC[2]:RC[106])"
Range("D15").Select
Selection.AutoFill Destination:=Range("D15:D88")
Range("D15:D88").Select
Range("B12").Select
Range(Selection, Selection.End(xlDown)).Select
Range("B15").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveWindow.SmallScroll Down:=-54
Range("D15").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
ActiveWindow.SmallScroll Down:=60
Range("B85:B88").Select
Selection.Copy
Range("D85:D88").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("G88").Select
End Sub
这是模块 2 中的代码:
Option Explicit
Public Sub ImportPrices()
Dim RawData As String
Dim RawFile As String
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
RawData = Sheets("Prices").Range("O1")
RawFile = Sheets("Prices").Range("O2")
If Dir(RawData) <> "" Then
Workbooks.Open Filename:=RawData
Range("a:i").Copy
ThisWorkbook.Activate
Sheets("Prices").Range("A1").PasteSpecial
Application.CutCopyMode = False
Windows(RawFile).Close savechanges:=False
Else
MsgBox "Please make sure " & RawFile & " is in the Raw Data folder!"
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Calculate
End
End If
Calculate
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
这是宏 RefreshAllPMWS:
Public Sub RefreshAllPMWS()
Dim objRange As Range
Dim objSheet As Worksheet
For Each objSheet In ThisWorkbook.Worksheets
If InStr(1, objSheet.Range("A1").Value2, "<?") Then
RefreshNormalSheet objSheet.Name
End If
Next
Application.Calculate
RefreshAllPivotCache
Application.Calculate
ImportPrices
End Sub
这是 RefreshAllPivotCache 的代码
Public Sub RefreshAllPivotCache()
Dim oCache As PivotCache
Application.Calculate
For Each oCache In ThisWorkbook.PivotCaches
'do not refresh pricing profile if skipped
oCache.Refresh
Next
End Sub
解决方案
推荐阅读
- c# - 如何将字符串数组拆分为新的字符串数组并删除重复项
- redux - 更新动态深度嵌套reducer
- node.js - 为什么 Express onError 处理程序只处理“监听”错误?
- python-3.x - 构建kivy时调用的类的参数
- c# - EF Core 查询在真实数据库中运行良好,但测试失败
- mqtt - 如何将 IoT 中心消息发送到 MQTT 代理?
- python - python连接到sql server
- ios - 在图像上绘制以标记特定对象并保存到照片库
- sql - 使用 Id 的 XML 查询和检索节点值
- python-3.x - 在 seaborn matplotlib python 中面临 add_legend() 的问题