lundi 20 avril 2015

Overflow error in VBA

I am getting an overflow error in the following piece of code:

Sub Button3_Click()
Dim inc As Integer
Dim i, j As Integer

Dim sem_new As Double
Dim aff_new As Double
Dim imu_new As Double


'Initialising Lever variables
 sem_new = Sheets("Front end").Range("C6").Value
 aff_new = Sheets("Front end").Range("D6").Value
 imu_new = Sheets("Front end").Range("E6").Value
 'Initializing increment variable
 inc = 1
 If Sheets("Control Sheet").Range("D5").Value = "Overall" Then
 MsgBox ("Overall")
 Else
 For i = 8 To 408
 j = 3
 sem_new = sem_new + 10000
 aff_new = aff_new + 10000
 imu_new = imu_new - 10000
 ****Sheets("Front end").Select
 Sheets("Front end").Cells(i, j).Value = ((Sheets("Front end").Range("F6").Value * ((sem_new / Sheets("Front end").Range("C6").Value) ^ Sheets("Front end").Range("H6").Value) * Sheets("Front end").Range("K6").Value) - (Sheets("Front end").Range("F6").Value * Sheets("Front end").Range("K6").Value)) / (inc * 10000)****
 j = j + 1
 Sheets("Front end").Cells(i, j).Value = ((Sheets("Front end").Range("F6").Value * ((aff_new / Sheets("Front end").Range("D6").Value) ^ Sheets("Front end").Range("I6").Value) * Sheets("Front end").Range("K6").Value) - (Sheets("Front end").Range("F6").Value * Sheets("Front end").Range("K6").Value)) / (inc * 10000)
 j = j + 1
 Sheets("Front end").Cells(i, j).Value = ((Sheets("Front end").Range("F6").Value * ((imu_new / Sheets("Front end").Range("C6").Value) ^ Sheets("Front end").Range("J6").Value) * Sheets("Front end").Range("K6").Value) - (Sheets("Front end").Range("F6").Value * Sheets("Front end").Range("K6").Value)) / (inc * 10000)
 inc = inc + 1
 Next i
 End If

 End Sub

The code runs fine for 3 iterations but throws an overflow error on the fourth iteration.None of the numbers being used in the formula are beyond millions.The line at which the error comes is highlighted below.Any help would be appreciated

Aucun commentaire:

Enregistrer un commentaire