Skipping Specific Row Within a Loop

Chirolove

New Member
Joined
Jul 2, 2019
Messages
19
Good day all,

I have a quick question: I am running a loop which copies certain cells from rows according to an IF statement from a different worksheet.

My code runs from i = 9 to i = 61 but I need it to ignore row 21, as it meets the IF condition yet I still want it to be ignored, how would I go about doing so?

Thanks in advance :)

Code:
Sub CopyTasks ()

Application.ScreenUpdating = False

Dim i As Long
Dim r As Long
r = 12

For i = 9 To 61
    With Sheets("TimeSheet")
        If .Cells(i, "K").Value >= 0.1 And .Cells(i, "K").Value <= 24 Then
            .Cells(i, "B").Resize(, 2).Copy: Sheets("Monday").Cells(r, "C").PasteSpecial xlValues
            .Cells(i, "E").Copy: Sheets("Monday").Cells(r, "E").PasteSpecial xlValues
            .Cells(i, "G").Copy: Sheets("Monday").Cells(r, "I").PasteSpecial xlValues
            r = r + 5
        End If
    End With

Next
Application.ScreenUpdating = True
End Sub
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
One way:
Code:
Application.ScreenUpdating = False

Dim i As Long
Dim r As Long
r = 12

For i = 9 To 61
[COLOR=#ff0000]    If i <>21 Then[/COLOR]
         With Sheets("TimeSheet")
             If .Cells(i, "K").Value >= 0.1 And .Cells(i, "K").Value <= 24 Then
                 .Cells(i, "B").Resize(, 2).Copy: Sheets("Monday").Cells(r, "C").PasteSpecial xlValues
                 .Cells(i, "E").Copy: Sheets("Monday").Cells(r, "E").PasteSpecial xlValues
                 .Cells(i, "G").Copy: Sheets("Monday").Cells(r, "I").PasteSpecial xlValues
                 r = r + 5
             End If
         End With
[COLOR=#ff0000]     End If[/COLOR]
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hey Joe4,

Yeah, that's exactly what I needed, now I can get on with the rest of the code. I hadn't excluded rows before so I was unsure how to approach it.

Thanks a bunch and have a nice day!

One way:
Code:
Application.ScreenUpdating = False

Dim i As Long
Dim r As Long
r = 12

For i = 9 To 61
[COLOR=#ff0000]    If i <>21 Then[/COLOR]
         With Sheets("TimeSheet")
             If .Cells(i, "K").Value >= 0.1 And .Cells(i, "K").Value <= 24 Then
                 .Cells(i, "B").Resize(, 2).Copy: Sheets("Monday").Cells(r, "C").PasteSpecial xlValues
                 .Cells(i, "E").Copy: Sheets("Monday").Cells(r, "E").PasteSpecial xlValues
                 .Cells(i, "G").Copy: Sheets("Monday").Cells(r, "I").PasteSpecial xlValues
                 r = r + 5
             End If
         End With
[COLOR=#ff0000]     End If[/COLOR]
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
You are welcome.
Glad I was able to help!
:)
 
Upvote 0
Another way, particularly useful if there are several values to exclude:
Rich (BB code):
For i = 9 To 61
    Select Case i
      Case 10, 14 To 17, 21
        'Do nothing for the above values/ranges
      Case Else
        With Sheets("TimeSheet")
            If .Cells(i, "K").Value >= 0.1 And .Cells(i, "K").Value <= 24 Then
                .Cells(i, "B").Resize(, 2).Copy: Sheets("Monday").Cells(r, "C").PasteSpecial xlValues
                .Cells(i, "E").Copy: Sheets("Monday").Cells(r, "E").PasteSpecial xlValues
                .Cells(i, "G").Copy: Sheets("Monday").Cells(r, "I").PasteSpecial xlValues
                r = r + 5
            End If
        End With
    End Select
Next
 
Upvote 0
I ended up using this method for another document, I am quite familiar with the Case statement but I never thought about using it this way!

Thanks a bunch to both of you :)


Another way, particularly useful if there are several values to exclude:
Rich (BB code):
For i = 9 To 61
    Select Case i
      Case 10, 14 To 17, 21
        'Do nothing for the above values/ranges
      Case Else
        With Sheets("TimeSheet")
            If .Cells(i, "K").Value >= 0.1 And .Cells(i, "K").Value <= 24 Then
                .Cells(i, "B").Resize(, 2).Copy: Sheets("Monday").Cells(r, "C").PasteSpecial xlValues
                .Cells(i, "E").Copy: Sheets("Monday").Cells(r, "E").PasteSpecial xlValues
                .Cells(i, "G").Copy: Sheets("Monday").Cells(r, "I").PasteSpecial xlValues
                r = r + 5
            End If
        End With
    End Select
Next
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,849
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top