我不断收到错误消息,提示“编译错误:不使用下一步”

时间:2020-11-06 02:13:39

标签: excel vba loops compilation next

大家好。我是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

2 个答案:

答案 0 :(得分:0)

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

Sub / End Sub, 功能/结束功能 如果/如果结束。 带有/结尾为,或 枚举/结束枚举

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

Sub MySub()
    ' Here is my code
End Sub

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

For / Next Do / 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

答案 1 :(得分:0)

您错过了两个Next:

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