2017-06-26 1 views
0

Ich versuche Ausreißer aus vielen Datenspalten zu finden und zu entfernen, aber es löscht nicht die Zellen, die Ausreißer enthalten, wenn ich den Code ausführen. Ich habe versucht, nur "ColLength" innerhalb der ersten For-Schleife zu drucken, und das tat auch nichts. Ratschläge, wo ich falsch gelaufen bin oder wie ich es beheben könnte?VBA-Drucken in For-Schleife

Sub Outliers() 

Dim calc As Worksheet 
Set calc = ThisWorkbook.Sheets("Sheet2") 

Dim num As Double 
Dim x As Integer 
Dim y As Integer 
Dim colLength As Integer 

'Variables for upper fence, lower fence, first quartile, third quartile, and interquartile range 
Dim upper As Double 
Dim lower As Double 
Dim q1 As Double 
Dim q3 As Double 
Dim interquartRange As Double 

For y = 1 To y = 49 

'Find last row of the column 
colLength = calc.Cells(Rows.count, y).End(xlUp).Row 

'Calculate first and third quartiles 
q1 = Application.WorksheetFunction.Quartile(Range(Cells(2, y), Cells(colLength, y)), 1) 
q3 = Application.WorksheetFunction.Quartile(Range(Cells(2, y), Cells(colLength, y)), 3) 

interquartRange = q3 - q1 

'Calculate upper and lower fences 
upper = q3 + (1.5 * interquartRange) 
lower = q1 - (1.5 * interquartRange) 

For x = 2 To x = colLength 
    num = calc.Cells(x, y) 

    'Remove outliers 
    If num > upper Or num < lower Then 
     Range(calc.Cells(x, y)).ClearContents 
    End If 
Next x 

Next y 

End Sub 

Antwort

1

For y = 1 To y = 49 sollte For y = 1 To 49 sein. Ebenso sollte For x = 2 To x = colLengthFor x = 2 To colLength sein, dies

Versuchen Sie in einem neuen Modul und Sie werden den Unterschied sehen und verstehen;)

Dim Y As Long 

Sub SampleA() 
    For Y = 1 To Y = 49 
     Debug.Print Y 
    Next Y 
End Sub 

Sub SampleB() 
    For Y = 1 To 49 
     Debug.Print Y 
    Next Y 
End Sub