mercredi 19 juin 2019

VBA ignoring if statement condition

Im trying to automate updating an excel sheet. If a cell in a certain row, with a certain value in the first column is blank, then I need the code to copy the last cell in that row, and paste it 12 times over.

The problem is that the code line : ElseIf .Range("A" & k) = "r" is ignored and just copying every cell of every row, 12 times.

How do I fix this?

If I comment the following two lines out of the code, it runs perfectly, except it doesn't paste over twelve times like I need it to.

With WS
        For i = 3 To 5
         For k = 3 To 5
        If .Range("A" & k) = "y" And IsEmpty(Cells(i, j)) Then
            .Cells(i, j - 1).Copy .Cells(i, j - 1).Offset(0, 1)

            .Cells(i, j - 1).Offset(0, 1).Select
        ElseIf .Range("A" & k) = "s" And IsEmpty(Cells(i, j + 1)) Then
            .Cells(i, j).Copy .Cells(i, j).Offset(0, 1)

            .Cells(i, j).Offset(0, 1).Select
        ElseIf .Range("A" & k) = "n" Then GoTo NextIteration5

        ElseIf .Range("A" & k) = "m" And IsEmpty(Cells(i, j)) Then
            .Cells(i, j - 1).Copy .Cells(i, j - 1).Offset(0, 1)

            .Cells(i, j - 1).Offset(0, 1).Select


         ElseIf .Range("A" & k) = "r" And IsEmpty(Cells(i, j)) Then

            .Cells(i, j - 1).Copy .Range(.Cells(i, j), .Cells(i, j + 4))
        End If
NextIteration5

Aucun commentaire:

Enregistrer un commentaire