lundi 11 juillet 2016

VBA: Loop with if condition only works correctly until the first TRUE

I am currently coding a loop in VBA and for some reason it only works until the first "If" statement is true. After that, it also applies the macro to cells that are FALSE. I just don't find the cause of this. In my example, operations 1.-3. should only be performed for x = 12 and x = 25 yet the code performes the macro for all x >= 12 and x <= 25

I've been trying for a good three hours now to fix this code and find an answer somewhere on the Internet... :-( Would be very happy if you could help! Thanks a lot in advance!

Sub CreateReport()

Dim lastrow As Long
Dim x As Long

lastrow = Sheets("Overview").Cells(Rows.Count, 1).End(xlUp).Row

For x = 10 To lastrow

    If ActiveSheet.Range("A" & x).EntireRow.Hidden = False Then

        '1. Copy sheet once per visible row

        Sheets("Master").Select
        Sheets("Master").Copy After:=Sheets(Sheets.Count)

        '2. Paste company name

        ActiveSheet.Cells(4, 1).Value = Sheets("Overview").Cells(x, 1).Value

        '3. Name worksheet after company name

        ActiveSheet.Name = Cells(4, 1).Value

    End If


Next x


End Sub

Aucun commentaire:

Enregistrer un commentaire