如何跳过包含使用VBAstring的循环中的单元格

我试图复制和粘贴单元格,如果单元格d2 = QTR 1(也可以是QTR 2,并且如果行j中的行的月份= 1,2或3.但是,我的代码给我一个错误,如果如果这个单元格是空的,那么这个行会被跳过,但是如果这个单元格包含一个string(“Enter Start Date”),那么这个代码将停止,debugging器将会打开。跳过,如果我有一个单元格的string?

我曾尝试添加一个条件:

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

不幸的是,条件<>“[Enter Start Date]”不起作用…我也试过:

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

这也不pipe用。

有任何想法吗? 我的代码如下,我可以在图像中看到一个循环的例子。

 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 

在细胞(i,10)是“Est。Start Date”

在这里输入图像说明

你可以使用内build函数IsDate()

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

如果单元格包含的东西不是date,则会跳过该单元格。