0

I am getting an "Overflow" error on line 40 of the following script:

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

The script calculates the average of column "D", and when the value from "A" changes, puts the result in "H".

Example:

 1. "A"         "D"                "H"
 2. 07:36:16    8.4
 3. 07:36:16    3.4                 5.9
 4. 07:36:17    2.1
2

1 Answer 1

2

In your code above the line

ActiveCell.Offset(-1, 7).Value = objExcel.WorksheetFunction.SumIf(RngCrt, Criteria, RngVal) / objExcel.WorksheetFunction.CountIf(RngCrt, Criteria)

Is line 41, so I assume that an extra newline has been added to that sample by mistake because I'm quite sure that's the line that fails.

So the easy way to figure out why it fails is to look at the values being calculated, so try changing that line into something like:

value1 = objExcel.WorksheetFunction.SumIf(RngCrt, Criteria, RngVal)
value2 = objExcel.WorksheetFunction.CountIf(RngCrt, Criteria)
msgbox value1
msgbox value2
ActiveCell.Offset(-1, 7).Value = value1 / value2

I assume that the values shown in the messageboxes won't be what you expect and so will hopefully show you where there error is.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.