excel 我一直收到一个错误,说“编译错误:《没有下一个》

inb24sb2  于 2023-05-30  发布在  其他
关注(0)|答案(2)|浏览(187)

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

h5qlskok1#

VBA中有许多语句必须正确终止。比如说
Sub / End Sub、Function / End Function、If / End If。使用/结束于,或枚举/结束枚举
为了更好的代码可读性,语句和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
idv4meu8

idv4meu82#

你错过了两个下一个:

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

相关问题