我在以下脚本的第40行遇到“溢出”错误:
Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = "Excel|*.xls|All Files|*.*"
objDialog.FilterIndex = 1
objDialog.InitialDir = "D:\"
intResult = objDialog.ShowOpen
If intResult = 0 Then
Wscript.Quit
Else
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(objDialog.FileName)
End If
objExcel.Visible = True
Set objWorksheet2 = objWorkbook.Worksheets("Foaie1")
objWorksheet2.Activate
objExcel.ScreenUpdating = False
objExcel.Calculation = xlCalculationManual
Set RngVal = objWorksheet2.Range("D1").CurrentRegion.Columns(4)
Set RngCrt = RngVal.Offset(0, -3)
objWorksheet2.Select
objWorksheet2.Range("A2").Select
Do Until IsEmpty(ActiveCell)
If ActiveCell.Value <> ActiveCell.Offset(-1, 0).Value Then
Criteria = ActiveCell.Offset(-1, 0).Value
ActiveCell.Offset(-1, 7).Value = objExcel.WorksheetFunction.SumIf(RngCrt, Criteria, RngVal) / objExcel.WorksheetFunction.CountIf(RngCrt, Criteria)
End If
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Offset(-1, 7).Value = objExcel.WorksheetFunction.SumIf(RngCrt, Criteria, RngVal) / objExcel.WorksheetFunction.CountIf(RngCrt, Criteria)
objExcel.Calculation = xlCalculationAutomatic
objExcel.ScreenUpdating = True
脚本计算列“D”的平均值,当“A”的值发生变化时,将结果置于“H”。
示例:
1. "A" "D" "H"
2. 07:36:16 8.4
3. 07:36:16 3.4 5.9
4. 07:36:17 2.1
答案 0 :(得分:2)
在您的代码上方
ActiveCell.Offset(-1, 7).Value = objExcel.WorksheetFunction.SumIf(RngCrt, Criteria, RngVal) / objExcel.WorksheetFunction.CountIf(RngCrt, Criteria)
是第41行,所以我假设错误地在该示例中添加了一个额外的换行符,因为我很确定这行是失败的。
因此,找出失败原因的简单方法是查看正在计算的值,因此请尝试将该行更改为:
value1 = objExcel.WorksheetFunction.SumIf(RngCrt, Criteria, RngVal)
value2 = objExcel.WorksheetFunction.CountIf(RngCrt, Criteria)
msgbox value1
msgbox value2
ActiveCell.Offset(-1, 7).Value = value1 / value2
我假设消息框中显示的值不符合您的预期,因此希望能够显示出错误的位置。