在VBA中修复“编译错误:下一个没有for”错误?

时间:2017-12-06 06:40:46

标签: excel vba excel-vba

我似乎无法弄清楚这一点。我想搜索我的工作表,看看是否有“= REF!”如果有错误没有运行我的代码。问题在于,当我运行它时,代码中的错误围绕下一部分

展开
Sub logs()

Dim numberofsheets As Integer

For numberofsheets = 1 To 5

Set checkRange = Sheets("Final Four").Range("A1:Z100")
 If IsError(CheckCell) And _
       CVErr(CheckCell) = CVErr(2023) Then
Exit Sub
End If

Next

Set checkRange = Sheets("Top Left").Range("A1:Z100")
 If IsError(CheckCell) And _
       CVErr(CheckCell) = CVErr(2023) Then
Exit Sub
End If

Next

Set checkRange = Sheets("Bottom Left").Range("A1:Z100")
 If IsError(CheckCell) And _
        CVErr(CheckCell) = CVErr(2023) Then
Exit Sub
End If

Next

Set checkRange = Sheets("Top Right").Range("A1:Z100")
 If IsError(CheckCell) And _
       CVErr(CheckCell) = CVErr(2023) Then
Exit Sub
End If

Next

Set checkRange = Sheets("Bottom Right").Range("A1:Z100")
 If IsError(CheckCell) And _
       CVErr(CheckCell) = CVErr(2023) Then
Exit Sub
End If

ActiveSheet.EnableCalculation = True

lst = Sheets("data").UsedRange.Rows.Count
x = lst + 1

        ' Display a message when one of the designated cells has been
        ' changed.
        ' Place your code here.
        Sheets("data").Range("A" & x) = ActiveSheet.Range("I3")
        Sheets("data").Range("B" & x) = ActiveSheet.Range("I4")


End Sub

不知道该怎么做。我很陌生。

1 个答案:

答案 0 :(得分:1)

我认为您正在寻找下面的For循环来实现您的代码:

Sub logs()

Dim Sht As Worksheet
Dim checkRange As Range, CheckCell As Range

For Each Sht In ThisWorkbook.Sheets ' loop through your worksheets
    With Sht
        Select Case .Name  ' check for the sheet.Name
            Case "Final Four", "Top Left", "Bottom Left", "Top Right", "Bottom Right"
                Set checkRange = .Range("A1:Z100") ' set the range for the current sheet

                For Each CheckCell In checkRange
                    If IsError(CheckCell) Then
                        If CheckCell.Value = CVErr(2023) Then Exit Sub

                        ' you can use the following syntax as well
                        If CheckCell.Value = CVErr(xlErrRef) Then Exit Sub
                    End If
                Next CheckCell
                Set checkRange = Nothing
            Case Else
                ' do nothing

        End Select
    End With
Next Sht

' rest of your code


End Sub