[英]How can I skip a cells in a loop containing a string using VBA
我正在嘗試復制和粘貼單元格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
不幸的是,條件<>“ [輸入開始日期]”不起作用...我也嘗試過:
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)是“預計開始日期”
您可以使用VBA內置函數IsDate()
If IsDate(cells(i,10)) then
'Do stuff
End If
如果單元格中包含不是日期的內容,它將跳過該日期。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.