繁体   English   中英

VBA-在For循环中打印

[英]VBA- Printing in For loop

我正在尝试从多列数据中查找和删除异常值,但是在运行代码时,它并没有清除包含异常值的单元格。 我尝试只在第一个For循环中打印“ colLength”,但也没有执行任何操作。 关于我哪里出了问题或如何解决的建议?

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

For y = 1 To y = 49应该是For y = 1 To 49 同样, For x = 2 To x = colLength应该是For x = 2 To colLength

在一个新的模块中尝试一下,您将看到并理解其中的区别;)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM