如果和Do Until Loop EXCEL VBA

Isr*_*ikh 5 excel vba if-statement excel-vba

VBA的新手,如果有人可以帮助我,我在这里做错了什么.尝试运行循环以查找特定文本,启动循环然后在特定点停止.循环是这样的,我希望它在我的工作表中复制下面的一些值,因此a是55.我面临错误Block IF没有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 Sub
Run Code Online (Sandbox Code Playgroud)

Sha*_*ado 2

除了我在帖子评论中提到的问题之外,如果我理解正确的话,您想要循环 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
Run Code Online (Sandbox Code Playgroud)