Search code examples
vbaloopsexcelskip

How can I skip a cells in a loop containing a string using VBA


I'm trying to copy and paste cells if cell d2 = Qtr 1 (can also be Qtr 2, and if the month in the rows in column j = 1, 2, or 3. However, my code is giving me an error if instead of a date, the cell has a string. If the cell is empty the row gets skipped, but if the cell contains a string ("Enter Start Date") then the code stops and the debugger opens. How can I ask my code to skip if I have a string on a cell?

I have tried adding a conditional:

if cells(2,4) = "Qtr 1" And Month(cells(i,10)) = 2 And cells(i,10) <> "[Enter Start Date]" then

Unfortunately, the conditional <> "[Enter Start Date]" does not work... I've also tried:

if cells(2,4) = "Qtr 1" And Month(cells(i,10)) = 2 Andcells(i,10).numberFormat = "m/d/yyyy" then

This doesn't work either.

Any ideas? My code is below and an example of what I'm looping through can be seen in the image.

Sub copyQtr()
Dim i As Long
Dim j As Long
Dim sheetName As String

Dim LastCol As Integer
Dim LastRow As Integer

Sheets("Activities").Activate

LastRow = Cells(Rows.Count, 10).End(xlUp).Row
LastCol = Cells(10, Columns.Count).End(xlToLeft).Column

sheetName = Sheets("Activities").Cells(2, 4)
Cells(4, 1) = sheetName

j = 11
For i = 11 To LastRow

If Cells(2, 4) = "Qtr 1" And Month(Cells(i, 10)) = 1 Then
Sheets("Activities").Range(Cells(i, 2), Cells(i, 12)).Copy
Sheets(sheetName).Cells(j, 2).PasteSpecial
j = j + 1
ElseIf Cells(2, 4) = "Qtr 1" And Month(Cells(i, 10)) = 2 Then
Sheets("Activities").Range(Cells(i, 2), Cells(i, 12)).Copy
Sheets(sheetName).Cells(j, 2).PasteSpecial
j = j + 1
ElseIf Cells(2, 4) = "Qtr 1" And Month(Cells(i, 10)) = 3 Then
Sheets("Activities").Range(Cells(i, 2), Cells(i, 12)).Copy
Sheets(sheetName).Cells(j, 2).PasteSpecial
j = j + 1

ElseIf Cells(2, 4) = "Qtr 2" And Month(Cells(i, 10)) = 4 Then
Sheets("Activities").Range(Cells(i, 2), Cells(i, 12)).Copy
Sheets(sheetName).Cells(j, 2).PasteSpecial
j = j + 1
ElseIf Cells(2, 4) = "Qtr 2" And Month(Cells(i, 10)) = 5 Then
Sheets("Activities").Range(Cells(i, 2), Cells(i, 12)).Copy
Sheets(sheetName).Cells(j, 2).PasteSpecial
j = j + 1
ElseIf Cells(2, 4) = "Qtr 2" And Month(Cells(i, 10)) = 6 Then
Sheets("Activities").Range(Cells(i, 2), Cells(i, 12)).Copy
Sheets(sheetName).Cells(j, 2).PasteSpecial
j = j + 1

ElseIf Cells(2, 4) = "Qtr 3" And Month(Cells(i, 10)) = 7 Then
Sheets("Activities").Range(Cells(i, 2), Cells(i, 12)).Copy
Sheets(sheetName).Cells(j, 2).PasteSpecial
j = j + 1
ElseIf Cells(2, 4) = "Qtr 3" And Month(Cells(i, 10)) = 8 Then
Sheets("Activities").Range(Cells(i, 2), Cells(i, 12)).Copy
Sheets(sheetName).Cells(j, 2).PasteSpecial
j = j + 1
ElseIf Cells(2, 4) = "Qtr 3" And Month(Cells(i, 10)) = 9 Then
Sheets("Activities").Range(Cells(i, 2), Cells(i, 12)).Copy
Sheets(sheetName).Cells(j, 2).PasteSpecial
j = j + 1

ElseIf Cells(2, 4) = "Qtr 4" And Month(Cells(i, 10)) = 10 Then
Sheets("Activities").Range(Cells(i, 2), Cells(i, 12)).Copy
Sheets(sheetName).Cells(j, 2).PasteSpecial
j = j + 1
ElseIf Cells(2, 4) = "Qtr 4" And Month(Cells(i, 10)) = 11 Then
Sheets("Activities").Range(Cells(i, 2), Cells(i, 12)).Copy
Sheets(sheetName).Cells(j, 2).PasteSpecial
j = j + 1
ElseIf Cells(2, 4) = "Qtr 4" And Month(Cells(i, 10)) = 12 And Cells(i, 10).NumberFormat = "m/d/yyyy" Then
Sheets("Activities").Range(Cells(i, 2), Cells(i, 12)).Copy
Sheets(sheetName).Cells(j, 2).PasteSpecial
j = j + 1

End If
Next


End Sub

Where cells(i,10) are "Est. Start Date"

enter image description here


Solution

  • You can use the VBA built in function IsDate()

    If IsDate(cells(i,10)) then
        'Do stuff
    End If
    

    If the cell contains something thats not a date it will skip that one.