如何使用VBA跳过包含字符串的循环中的单元格

时间:2014-10-28 15:14:26

标签: excel vba loops excel-vba skip

我正在尝试复制和粘贴单元格,如果单元格d2 = Qtr 1(也可以是Qtr 2,如果j列中的行中的月份= 1,2或3.但是,我的代码给了我如果不是日期,则单元格有一个字符串。如果单元格为空,则跳过该行,但如果单元格包含字符串(“输入开始日期”),则代码停止,调试器打开。我怎么能如果我在单元格上有字符串,请让我的代码跳过?

我尝试添加条件:

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

不幸的是,条件&lt;&gt; “[输入开始日期]”不起作用......我也尝试过:

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

这也不起作用。

有什么想法吗?我的代码在下面,我可以在图像中看到我正在循环的示例。

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。开始日期”

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用VBA内置函数IsDate()

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

如果单元格包含的内容不是日期,则会跳过该单元格。