Excel VBA方法无法运行并且不返回任何错误

时间:2015-03-09 19:21:49

标签: vba excel-vba large-data excel

我打算让我的代码搜索一个充满数据的excel电子表格,并返回p值低于0.05的整行。但是,我没有收到任何语法错误,代码看起来正确。我正在使用大型数据集,~780000行条目,所以我不知道这是否是导致错误的原因。代码应该通读,如果满足上述条件,则返回整行,否则该行应该被忽略。这里,代码不返回任何内容并且不提供任何错误。

Sub GrabRelaventData()
    Dim i As Long, j As Long
    Dim s1 As Worksheet, s2 As Worksheet
    Set s1 = Sheets("RNASeq EH developmental 10 hits")
    Set s2 = Sheets("pVal checks")
    For i = 1 To 787731
        If s1.Cells(i, 10) > 0.05 And s1.Cells(i, 16) > 0.05 And s1.Cells(i, 22) > 0.05 And s1.Cells(i, 26) > 0.05 Then
            Exit For
        Else
            For j = 1 To 39
                s2.Cells(i, j).Value = s1.Cells(i, j).Value
            Next j
        End If
    Next i
End Sub

1 个答案:

答案 0 :(得分:0)

根据你的工作,我认为你已经改变了你的逻辑。如果值小于而不是.05,则将IF设置为你想要做的。如果值更大,您将进入NEXT并循环。

当你使用EXIT FOR时,它不会进入下一个循环,它表示你完成了。某些语言(VB.net)有一个Continue For语句,它将引导您完成循环。假设你的其余代码是正确的,试试这个:

Sub GrabRelaventData()
    Dim i As Long, j As Long
    Dim s1 As Worksheet, s2 As Worksheet
    Set s1 = Sheets("RNASeq EH developmental 10 hits")
    Set s2 = Sheets("pVal checks")
    For i = 1 To 787731
    'Flip your signs - you want to do something when the p-value is LESS THAN, 
'not avoid doing something when it is greater than.
        If s1.Cells(i, 10) < 0.05 And s1.Cells(i, 16) < 0.05 And s1.Cells(i, 22) < 0.05 And s1.Cells(i, 26) < 0.05 Then
            '"Exit For" will take you to the next i - you want use the j loop if the condition is true.
       ' Else - No need for the else this way.
            For j = 1 To 39
                s2.Cells(i, j).Value = s1.Cells(i, j).Value
            Next j
        End If
    Next i
End Sub

这假设您希望所有p值都是&lt; 0.05。如果你想在任何p值<&lt; .05你会使用OR而不是AND。 祝你好运!