【问题标题】:I keep getting an error that says "Compile error: For without Next"我不断收到一条错误消息,显示“编译错误:对于没有下一步”
【发布时间】:2021-02-18 19:01:47
【问题描述】:

大家好。我是 VBA 新手,我不断收到一条错误消息,显示“编译错误: 对于没有下一个”

Sub Aplhabetical_Testing()

Dim ws As Worksheet
Dim ticker As String
Dim vol As Integer
Dim year_open As Double
Dim year_close As Double
Dim yearly_change As Double
Dim percent_change As Double
Dim total_stock_volume As Double


    For Each ws In Worksheet

        ws.Range("I1").Value = "Ticker"
        ws.Range("J1").Value = "Yearly Change"
        ws.Range("K1").Value = "Percent Change"
        ws.Range("L1").Value = "Total Stock Volume"
        
        ws.Range("P1").Value = "Ticker"
        ws.Range("Q1").Value = "Value"
        ws.Range("O2").Value = "Greatest % Increase"
        ws.Range("O3").Value = "Greatest % Decrease"
        ws.Range("O4").Value = "Greatest Total Volume"


    For i = 2 To RowCount
    j = 0
    total = 0
    Change = 0
    Start = 2
    
    
         If Cells(i + 1, 1).Value <> Cells(i, 7).Value Then
            total = total + Cells(i, 7).Value
                    Range("I" & 2 + j).Value = Cells(i, 1).Value
                    Range("j" & 2 + j).Value = 0
                    Range("K" & 2 + j).Value = "%" & 0
                    Range("L" & 2 + j).Value = 0
        Else
            If Cells(Start, 3) = 0 Then
                For find_value = Start To i
                    If Cells(find_value, 3).Value <> 0 Then
                            Start = find_value
                            Exit For
                        End If
                    Next find_value
End If

                
                Change = (Cells(i, 6) - Cells(Start, 3))
                percentChange = Round((Change / Cells(Start, 3) * 100), 2)
                Start = i + 1
                
                Range("I" & 2 + j).Value = Cells(i, 1).Value
                Range("j" & 2 + j).Value = Round(Change, 2)
                Range("K" & 2 + j).Value = "%" & percentChange
                Range("L" & 2 + j).Value = total
                
                Select Case Change
                    Case Is > 0
                        Range("j" & 2 + j).Interior.ColorIndex = 4
                    Case Is < 0
                        Range("j" & 2 + j).Interior.ColorIndex = 3
                    Case Else
                        Range("j" & 2 + j).Interior.ColorIndex = 0
                    End Select
      End If
      
      
 
          
          
End Sub

【问题讨论】:

标签: excel vba loops compilation next


【解决方案1】:

VBA 中有许多语句必须正确终止。例如,

子/结束子, 功能/结束功能, 如果/结束如果。 以 / 结束,或 枚举/结束枚举

为了更好的代码可读性,语句和End 之间的所有内容都应该缩进,如下所示:-

Sub MySub()
    ' Here is my code
End Sub

If 1 < 2 Then
    ' Here is what to do in that case
End If

For / NextDo / Loop 的工作方式完全相同。例如,

For i = 1 to 10
    ' code to be executed *i* times
Next i

这些概念可以嵌套。这是一个例子。

Private Sub MySub()
    Dim i As Integer
    For i = 1 to 10
        If i = 5 then
            Debug.Print "Half done"
        End if
    Next i
End Sub

【讨论】:

    【解决方案2】:

    你错过了两个下一个:

    Sub Aplhabetical_Testing()
    
        Dim ws As Worksheet
        Dim ticker As String
        Dim vol As Integer
        Dim year_open As Double
        Dim year_close As Double
        Dim yearly_change As Double
        Dim percent_change As Double
        Dim total_stock_volume As Double
    
        For Each ws In Worksheet    ' Worksheets?
            ws.Range("I1").Value = "Ticker"
            ws.Range("J1").Value = "Yearly Change"
            ws.Range("K1").Value = "Percent Change"
            ws.Range("L1").Value = "Total Stock Volume"
            
            ws.Range("P1").Value = "Ticker"
            ws.Range("Q1").Value = "Value"
            ws.Range("O2").Value = "Greatest % Increase"
            ws.Range("O3").Value = "Greatest % Decrease"
            ws.Range("O4").Value = "Greatest Total Volume"
    
            For i = 2 To RowCount
                j = 0
                total = 0
                Change = 0
                Start = 2
        
                If Cells(i + 1, 1).Value <> Cells(i, 7).Value Then
                    total = total + Cells(i, 7).Value
                    Range("I" & 2 + j).Value = Cells(i, 1).Value
                    Range("j" & 2 + j).Value = 0
                    Range("K" & 2 + j).Value = "%" & 0
                    Range("L" & 2 + j).Value = 0
                Else
                    If Cells(Start, 3) = 0 Then
                        For find_value = Start To i
                            If Cells(find_value, 3).Value <> 0 Then
                                Start = find_value
                                Exit For
                            End If
                        Next find_value
                    End If
                    
                    Change = (Cells(i, 6) - Cells(Start, 3))
                    percentChange = Round((Change / Cells(Start, 3) * 100), 2)
                    Start = i + 1
                    
                    Range("I" & 2 + j).Value = Cells(i, 1).Value
                    Range("j" & 2 + j).Value = Round(Change, 2)
                    Range("K" & 2 + j).Value = "%" & percentChange
                    Range("L" & 2 + j).Value = total
                    
                    Select Case Change
                        Case Is > 0
                            Range("j" & 2 + j).Interior.ColorIndex = 4
                        Case Is < 0
                            Range("j" & 2 + j).Interior.ColorIndex = 3
                        Case Else
                            Range("j" & 2 + j).Interior.ColorIndex = 0
                    End Select
                End If 
            ' Missing Next
            Next
        ' Missing Next
        Next
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2018-05-23
      • 2021-07-15
      • 1970-01-01
      • 2012-06-04
      • 2015-09-02
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多