Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If and Do Until Loop EXCEL VBA

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

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
like image 367
Isra Shaikh Avatar asked Jan 24 '17 17:01

Isra Shaikh


Video Answer


1 Answers

Indenting is the way forward, you have a for statement with no next and an if with no 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
like image 137
Preston Avatar answered Sep 20 '22 15:09

Preston