excel 如何修复“编译错误:结束If而不阻塞If,”

lhcgjxsq  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(146)

我得到了这段代码,但不知什么原因,“End If”找不到块If语句。我尝试了几种解决方案,但都无法让它工作。如有任何建议,我将不胜感激。
我不知道这个问题还有什么需要说的,所以如果还有什么需要,请告诉我。
这段代码是我编辑的,我对VBA还是个新手,还在学习。

If cStatus Is Nothing Then 'This is the block If statement that the End If should be tied to. 
       Set cStatus = Sheet1.Range("N1:N1000").Find(what:="Done, On-going")
         
         Do While Len(cStatus.Value) > 0
          Select Case LCase(cStatus.Value)
            Case "done": Set wsDest = Sheet4
            Case "on-going": Set wsDest = Sheet2
            Case Else: Set wsDest = Nothing
          End Select
        
          If Not wsDest Is Nothing Then
               cStatus.EntireRow.Cut _
               Destination:=wsDest.Cells(Rows.Count, "A").End(xlUp).Offset(1)
          End If 'Error does move to this one too. 
        
      End If 'The error is with this one here. 

  'Set cStatus = cStatus.Offset(1, 0) 'next source row
  Loop
End Sub
798qvoo8

798qvoo81#

如果我使用“Rubberduck”https://test.rubberduckvba.com/Indenter进行缩进,则会得到以下结果(注意,我添加了Sub Test以匹配End Sub):

Sub Test()
    If cStatus Is Nothing Then 'This is the block If statement that the End If should be tied to.
        Set cStatus = Sheet1.Range("N1:N1000").Find(what:="Done, On-going")
        
        Do While Len(cStatus.Value) > 0
            Select Case LCase(cStatus.Value)
                Case "done": Set wsDest = Sheet4
                Case "on-going": Set wsDest = Sheet2
                Case Else: Set wsDest = Nothing
            End Select
            
            If Not wsDest Is Nothing Then
                cStatus.EntireRow.Cut _
                Destination:=wsDest.Cells(Rows.Count, "A").End(xlUp).Offset(1)
            End If 'Error does move to this one too.
            
        End If 'The error is with this one here.
        
        'Set cStatus = cStatus.Offset(1, 0) 'next source row
    Loop
End Sub

看看Do WhileLoop的缩进量如何不同,第二个End If的缩进量如何与第一个If的缩进量不匹配。

Sub Test()
    If cStatus Is Nothing Then 'This is the block If statement that the End If should be tied to.
        Set cStatus = Sheet1.Range("N1:N1000").Find(what:="Done, On-going")
        
        Do While Len(cStatus.Value) > 0
            Select Case LCase(cStatus.Value)
                Case "done": Set wsDest = Sheet4
                Case "on-going": Set wsDest = Sheet2
                Case Else: Set wsDest = Nothing
            End Select
            
            If Not wsDest Is Nothing Then
                cStatus.EntireRow.Cut _
                Destination:=wsDest.Cells(Rows.Count, "A").End(xlUp).Offset(1)
            End If 'Error does move to this one too.
            
            'Set cStatus = cStatus.Offset(1, 0) 'next source row
        Loop
    End If 'The error is with this one here.
End Sub

顺便说一句,你还没有解释你的逻辑,但我想也许你实际上想要的是也许这?:

Sub Test()
    If cStatus Is Nothing Then
        Set cStatus = Sheet1.Range("N1:N1000").Find(what:="Done, On-going")
    End If
    
    Do While Len(cStatus.Value) > 0
        Select Case LCase(cStatus.Value)
            Case "done": Set wsDest = Sheet4
            Case "on-going": Set wsDest = Sheet2
            Case Else: Set wsDest = Nothing
        End Select
        
        If Not wsDest Is Nothing Then
            cStatus.EntireRow.Cut _
            Destination:=wsDest.Cells(Rows.Count, "A").End(xlUp).Offset(1)
        End If
        
        Set cStatus = cStatus.Offset(1, 0) 'next source row
    Loop
    
End Sub

相关问题