代码之家  ›  专栏  ›  技术社区  ›  7XCOLE

我的VBA Excel 2013代码未编译

  •  0
  • 7XCOLE  · 技术社区  · 7 年前

    当我运行此代码时:

    Private Sub Workbook_Open()
    Dim i As Integer
    Dim j As Integer
    Dim range1 As Integer
    Dim range2 As Integer
    range1 = 53
    range2 = 102
    
    For i = range1 To range2
        For j = (range1 - 50) To (range2 - 50)
    
            If Cells(2, i) = Cells(2, j) Then
                If Cells(7, i) > Cells(7, j) Then
                Cells(2, i).Interior.ColorIndex = 37 'Went up; Green
                ElseIf Cells(7, i) = Cells(7, j) Then
                Cells(2, i).Interior.ColorIndex = 37 'No change; Grey
                Else
                Cells(2, i).Interior.ColorIndex = 37 'Went down; Red
            End If
    
        Next j
        If Cells(2, i).Interior.ColorIndex = 0 Then 'Hasn't Changed; No Fill
        Cells(2, i).Interior.ColorIndex = 37 'New Song; Blue
        End If
    
    Next i
    End Sub
    

    出现一个错误,说明:
    编译错误:Next不带For
    然而,每个下一个肯定都有一个For。
    那么我哪里做错了?
    注意:37只是填充数字,我知道它看起来是浅蓝色的。

    1 回复  |  直到 6 年前
        1
  •  1
  •   YowE3K    7 年前

    以一致的方式缩进代码可以得到以下结果:

    Private Sub Workbook_Open()
        Dim i As Integer
        Dim j As Integer
        Dim range1 As Integer
        Dim range2 As Integer
        range1 = 53
        range2 = 102
    
        For i = range1 To range2
            For j = (range1 - 50) To (range2 - 50)
    
                If Cells(2, i) = Cells(2, j) Then
                    If Cells(7, i) > Cells(7, j) Then
                        Cells(2, i).Interior.ColorIndex = 37 'Went up; Green
                    ElseIf Cells(7, i) = Cells(7, j) Then
                        Cells(2, i).Interior.ColorIndex = 37 'No change; Grey
                    Else
                        Cells(2, i).Interior.ColorIndex = 37 'Went down; Red
                    End If
    
                    Next j ' <--- This Next has no For associated with it
    
                    If Cells(2, i).Interior.ColorIndex = 0 Then 'Hasn't Changed; No Fill
                        Cells(2, i).Interior.ColorIndex = 37 'New Song; Blue
                    End If
                Next i
            End Sub
    

    通过缩进级别,可以很快判断 Next j 没有 For 在当前 If 块这就是为什么你会出错。

    我怀疑你打算 End If 就在那之前 下一个j 因此,您的代码如下所示:

    Private Sub Workbook_Open()
        Dim i As Integer
        Dim j As Integer
        Dim range1 As Integer
        Dim range2 As Integer
        range1 = 53
        range2 = 102
    
        For i = range1 To range2
            For j = (range1 - 50) To (range2 - 50)
    
                If Cells(2, i) = Cells(2, j) Then
                    If Cells(7, i) > Cells(7, j) Then
                        Cells(2, i).Interior.ColorIndex = 37 'Went up; Green
                    ElseIf Cells(7, i) = Cells(7, j) Then
                        Cells(2, i).Interior.ColorIndex = 37 'No change; Grey
                    Else
                        Cells(2, i).Interior.ColorIndex = 37 'Went down; Red
                    End If
                End If    
    
            Next j
    
            If Cells(2, i).Interior.ColorIndex = 0 Then 'Hasn't Changed; No Fill
                Cells(2, i).Interior.ColorIndex = 37 'New Song; Blue
            End If
        Next i
    End Sub