为什么这个程序应该对每列进行求和,仅在一列之后停止?

The*_*Man 2 excel vba excel-vba excel-2010

该程序创建一个数字表,然后尝试逐行加总.我正在IsBlank()测试最顶层的单元格是否为空白.如果它是空白的,它应该结束循环,但如果不是,循环应该继续.但是,它在第​​一次循环后不断结束.这是为什么?

我觉得这很明显.

编辑:我应该注意到整个"反"的东西都在那里,因为如果这有效的话我会开始玩这个.它不起作用,所以我在这里!

Option Explicit
Dim Counter As Long
Dim i As Long
Dim col As Long
Dim row As Long

Sub SumRange()
    For col = 1 To 8
        For row = 1 To 6
            Cells(row, col) = Rnd
        Next row
    Next col
    Counter = 6
    For i = 1 To 9
        If IsEmpty(Cells(1, i)) = False Then
            Cells(Counter + 1, i) = Application.WorksheetFunction.Sum(Range(Cells(1, i), Cells(Counter, i)))
        Else
            End If
        End
    Next
    MsgBox Cells(4, 5)
End Sub
Run Code Online (Sandbox Code Playgroud)

lc.*_*lc. 5

有两个问题:

  1. End语句不正确.如果我没记错的话,就End意味着结束程序.你必须明确说明您是什么结局(End If,End With,...).在这种情况下你的意思是End If.

  2. 您需要使用Exit For跳出for循环.我认为你的意思是你当前的End If陈述.

我不确定你要做什么,但你也可以考虑使用带有条件的while循环,While Not IsEmpty(Cells(1, i))然后i从循环内增加计数器.对我来说,这感觉比跳过它的for循环好一点.