从当前活动单元格运行代码,不覆盖以前的单元格

时间:2016-05-05 12:51:20

标签: excel vba excel-vba

我开发了以下代码来选择下一个空单元格,但它不会运行我的其余代码来检查列。

当我运行代码时,它会覆盖我以前的工作。我希望代码找到下一个空单元格并从那里运行,忽略之前的单元格。在尝试选择第一个空单元格之前,代码的下半部分已经工作。

Sub Outcome()
Dim Action As String
Dim Status As String
Dim Module As String
Dim Outcome As String
Dim x As Integer
Dim a As Integer

Application.Goto Cells(Rows.Count, "J").End(xlUp).Offset(1), Scroll:=True

For x = Active.Cell To 500 ---> (First blank cell to 500)
Action = Cells(x, 5).Value
Status = Cells(x, 8).Value
Module = Cells(x, 9).Value
Outcome = Cells(x, 10).Value


If Action = "" Then Exit Sub

If Action = " Page was incorrectly blocked" And Status = "NOT BLOCKED" And Module = "NOT BLOCKED" Then Outcome = "No Action" Else
If Action = " Page was incorrectly blocked" And Status = "BLOCKED" And Module = "caravan" Then Outcome = "Check" Else
If Action = " Page was incorrectly blocked" And Status = "BLOCKED" And Module = "Holiday" Then Outcome = "Refer to Nominum" Else
If Action = " Page was incorrectly blocked" And Status = "" And Module = "" Then Outcome = "Check" Else


If Action = " Page was incorrectly blocked (caravan)" And Status = "NOT BLOCKED" And Module = "NOT BLOCKED" Then Outcome = "No Action" Else
If Action = " Page was incorrectly blocked (caravan)" And Status = "BLOCKED" And Module = "caravan" Then Outcome = "Check" Else
If Action = " Page was incorrectly blocked (caravan)" And Status = "BLOCKED" And Module = "Holiday" Then Outcome = "Refer to Nominum" Else
If Action = " Page was incorrectly blocked (caravan)" And Status = "" And Module = "" Then Outcome = "Check" Else


If Action = " Page was incorrectly blocked (Malware)" And Status = "NOT BLOCKED" And Module = "NOT BLOCKED" Then Outcome = "No Action" Else
If Action = " Page was incorrectly blocked (Malware)" And Status = "BLOCKED" And Module = "Holiday" Then Outcome = "Refer to Nominum" Else
If Action = " Page was incorrectly blocked (Malware)" And Status = "BLOCKED" And Module = "caravan" Then Outcome = "Check" Else
If Action = " Page was incorrectly blocked (Malware)" And Status = "" And Module = "" Then Outcome = "Check" Else


If Action = " This page should be blocked." And Status = "NOT BLOCKED" And Module = "NOT BLOCKED" Then Outcome = "Check" Else
If Action = " This page should be blocked." And Status = "BLOCKED" And Module = "caravan" Then Outcome = "No Action" Else
If Action = " This page should be blocked." And Status = "BLOCKED" And Module = "Holiday" Then Outcome = "Refer to Nominum" Else
If Action = " This page should be blocked." And Status = "" And Module = "" Then Outcome = "Check" Else

Next x

End Sub

1 个答案:

答案 0 :(得分:2)

简单地For x = Active.Cell To 500根本不起作用...... Active.Cell会假设Active是一个范围(但你从未声明过它)。或者这样做:

For x = Selection.Row To Selection.Row + 500

或者将其作为范围运行:

Dim xx as Variant
For each xx in Selection.Resize(500, 1).Cells

然后xx将成为单元格本身。

同样清理你的巨大IF块...它以Else结束,并且应该触发语法错误! (但不幸的是......)

修改

对于您的4个If块:

If Action = " Page was incorrectly blocked" Or Action = " Page was incorrectly blocked (caravan)" Or Action = " Page was incorrectly blocked (Malware)" Or Action = " This page should be blocked." Then
  If Status = "BLOCKED" And Module = "Holiday" Then
    Outcome = "Refer to Nominum"
  ElseIf Status = "" And Module = "" Then
    Outcome = "Check"
  ElseIf Status = "NOT BLOCKED" And Module = "NOT BLOCKED" Then
    If Action = " This page should be blocked." Then Outcome = "Check" Else Outcome = "No Action"
  ElseIf Status = "BLOCKED" And Module = "caravan" Then
    If Action = " This page should be blocked." Then Outcome = "No Action" Else Outcome = "Check"
  End If
End If