如何找出“ for”声明的声明

时间:2019-10-18 09:08:57

标签: excel vba

在我的代码中,我有一个名为

的语句
  For I = 1 To 200
    Range("C47").Value = Worksheets("HAZIDS").Cells(I + 5, 2).Value
    conscat = Range("F47")
    check2 = cons Like conscat
    check3 = cons Like "All"

当尝试运行代码时,出现错误提示

  

“没有下一个”

而且我似乎无法弄清楚该如何解决。

我尝试查找其他人如何解决此问题,并尝试下一个运气不好的下一条语句。

  Option Explicit

Private Sub CommandButton1_Click()

Dim I As Integer
Dim row As Integer
Dim before As String
Dim after As String
Dim cons As String
Dim conscat As String
Dim checks As String
Dim check2 As String
Dim check3 As String
Dim rowbefore As String
Dim columbefore As String
Dim rowafter As String
Dim columafter As String
Dim checkbefore As String
Dim checkafter As String
Dim Previouscellcontentbefore As Integer
Dim Previouscellcontentafter As Integer



Sheets("for calculations").Visible = True

cons = Application.InputBox(prompt:="Personnel; Environment; Assets; Reputation; All", Title:="Choose consequence (NB: Case sensitive)", Default:="All")
Worksheets("For calculations").Activate

Range("D37:I42").ClearContents
Range("L37:Q42").ClearContents
Range("C34").ClearContents

Select Case cons

    Case "All"
        Range("C34").Value = "Risk matrix shows all types of consequences"
    Case "Personnel"
        Range("C34").Value = "Risk matrix shows all types of Personnel consequences"
    Case "Environment"
        Range("C34").Value = "Risk matrix shows Environmental consequences"
    Case "Asset"
        Range("C34").Value = "Risk matrix shows Asset consequences"
    Case "Reputation"
        Range("C34").Value = "Risk matrix shows Reputation consequences"
End Select


For I = 1 To 200
    Range("C47").Value = Worksheets("HAZIDS").Cells(I + 5, 2).Value
    conscat = Range("F47")
    check2 = cons Like conscat
    check3 = cons Like "All"

If cons Like "All" Then
    check2 = True
End If

    If check2 Then

before = Range("D47")
after = Range("E47")
rowbefore = Mid(before, 2, 1)
columbefore = Mid(before, 4, 1)
rowafter = Mid(after, 2, 1)
columafter = Mid(after, 4, 1)

checkbefore = Not rowbefore Like "" And Not columbefore Like ""
checkafter = Not rowafter Like "" And Not columafter Like ""

    If checkbefore Then
        Range("C36").Select
        Previouscellcontentbefore = ActiveCell.Offset(CInt(rowbefore) + 1, CInt(columbefore) + 1)
        ActiveCell.Offset(CInt(rowbefore) + 1, CInt(columbefore) + 1) = Range("C47").Value & ", " & Previouscellcontentbefore

    If checkafter Then
        Range("K36").Select
        Previouscellcontentafter = ActiveCell.Offset(CInt(rowafter) + 1, CInt(columafter) + 1)
          ActiveCell.Offset(CInt(rowafter) + 1, CInt(columafter) + 1) = Range("C47").Value & ", " & Previouscellcontentafter

        End If
    End If
End If



End Sub

该代码应该检查特定输入的不同范围,并使用此给定的输入运行其余代码。

1 个答案:

答案 0 :(得分:5)

正如错误所言,您错过了下一步。

For I = 1 To 200 ' the code loops from here
    Range("C47").Value = Worksheets("HAZIDS").Cells(I + 5, 2).Value
    conscat = Range("F47")
    check2 = cons Like conscat
    check3 = cons Like "All"
Next I ' to here

没有Next,就不知道循环在哪里结束。