2014-10-28 23 views
0

我試圖複製和粘貼單元格,如果單元格d2 = Qtr 1(也可以是Qtr 2,並且如果在j列中的行中的月份=如果單元格是空的,行會被跳過,但如果單元格包含一個字符串(「Enter Start Date 。「),那麼代碼將停止,調試器打開我怎樣才能讓我的代碼來跳過,如果我有一個電池串如何跳過包含使用VBA的字符串的循環中的單元格

我試圖增加一個條件:?

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

不幸的是,條件<>「[En ter Start Date]「不起作用......我也試過:

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 

凡細胞(1,10)是 「預估開始日期」

enter image description here

+0

您是否嘗試過isDate()函數? http://www.techonthenet.com/excel/formulas/isdate.php – Alex 2014-10-28 15:27:51

+0

剛剛做了,它的工作。謝謝! – 2014-10-28 15:33:11

回答

1

您可以使用VBA內置函數IsDate()

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

如果單元格包含這不是一個日期,它會跳過那一個。

+1

工作。謝謝。忘了這個功能! – 2014-10-28 15:32:41

+0

歡迎您! – 2014-10-28 15:33:10

相關問題