If 和 Do until 循环 EXCEL VBA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41834632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:56:52  来源:igfitidea点击:

If and Do Until Loop EXCEL VBA

excelvbaexcel-vbaif-statement

提问by Isra Shaikh

New to VBA if someone could help me what im doing wrong here. Trying to run a loop such that it looks for a specific text, starts the loop then stops at a specific point. The loops is such that I want it to copy some values below in my sheet hence a is 55. Im facing the error Block IF without End If

如果有人可以帮助我在这里做错了什么,VBA 的新手。尝试运行循环以查找特定文本,开始循环然后在特定点停止。循环是这样的,我希望它在我的工作表中复制下面的一些值,因此 a 是 55。我面临错误 Block IF without End If

Here is the code:

这是代码:

Private Sub CommandButton3_Click()
For y = 1 To 15 Step 5
Dim x As Double
Dim a As Double
x = 1
a = 55
If Cells(x, y).Value = "Text1" Then
Do Until Cells(x, y).Value = "Text2"
Cells(a, y) = Cells(x, y).Value
Cells(a, y + 1) = Cells(x, y + 1)
x = x + 1
a = a + 1


Loop


End Sub

采纳答案by Shai Rado

Besides the issues I mentioned in the comments to your post, if I understood you correctly, you want to loop on cells at Column A, find the first "Text1", then copy all the cells to row 55 and below, until you find "Text2". If that's the case, try the code below :

除了我在您的帖子的评论中提到的问题,如果我理解正确,您想在 A 列的单元格上循环,找到第一个“Text1”,然后将所有单元格复制到第 55 行及下方,直到找到“文本 2”。如果是这种情况,请尝试以下代码:

Private Sub CommandButton3_Click()

Dim x As Long, y As Long
Dim a As Long
Dim LastRow As Long

With Worksheets("Sheet1") '<-- modify "Sheet1" to your sheet's name
    For y = 1 To 15 Step 5

        x = 1  '<-- reset x and a (rows) inside the columns loop
        a = 55 '<-- start pasting from row 55

        LastRow = .Cells(.Rows.Count, y).End(xlUp).Row
        While x <= LastRow '<-- loop until last row with data in Column y
            If .Cells(x, y).Value Like "Text1" Then
                Do Until .Cells(x, y).Value = "Text2"
                    .Cells(a, y).Value = .Cells(x, y).Value
                    .Cells(a, y + 1).Value = .Cells(x, y + 1).Value
                    x = x + 1
                    a = a + 1
                Loop
            End If
            x = x + 1
        Wend
    Next y
End With

End Sub

回答by User632716

Indenting is the way forward, you have a forstatement with no nextand an ifwith no End If:

缩进是前进的方向,你有一个for没有nextif没有的声明End If

Private Sub CommandButton3_Click()
    For y = 1 To 15 Step 5
        Dim x As Double
        Dim a As Double
        x = 1
        a = 55
        If Cells(x, y).Value = "Text1" Then
            Do Until Cells(x, y).Value = "Text2"
                Cells(a, y) = Cells(x, y).Value
                Cells(a, y + 1) = Cells(x, y + 1)
                x = x + 1
                a = a + 1
            Loop
        End If
    Next y
end sub