excel:遍历列跳过值

时间:2017-07-10 14:26:21

标签: excel vba excel-vba

当找到某个值时,有没有办法在循环内跳过一行?

例如,我有一个循环,它通过一个列复制日期并粘贴其值,并使用此代码在另一个工作表中再添加一天:

Sub Apoios()

Dim i As Long
Dim j As Long
Dim lastrow As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet

Set ws1 = Sheets("Plan3")
Set ws2 = Sheets("BASE_TOTAL")
Set ws3 = Sheets("APOIOS")

lastrow = ws2.Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To lastrow
    For j = 1 To 11
        Select Case True
            Case IsEmpty(ws2.Cells(i, j)):
                ws3.Cells(i, j) = ""

            Case Not IsEmpty(ws2.Cells(i, j)):
                If ws1.Cells(4, 3) <= ws2.Cells(i, 8) And ws2.Cells(i, 5).Value <> "APOIO" Then
                    ws3.Cells(i, 1).Value = ws2.Cells(i, 1)
                    ws3.Cells(i, 2).Value = ws2.Cells(i, 2)
                    ws3.Cells(i, 3).Value = ws2.Cells(i, 3)
                    ws3.Cells(i, 4).Value = ws2.Cells(i, 4)
                    ws3.Cells(i, 5).Value = "APOIO"
                    ws3.Cells(i, 6).Value = "-"
                    ws3.Cells(i, 7).Value = DateAdd("d", 1, ws2.Cells(i, 8).Value)
                    ws3.Cells(i, 8).Value = "-------------"
                    ws3.Cells(i, 9).Value = ws2.Cells(i, 9)
                    ws3.Cells(i, 10).Value = ws2.Cells(i, 10)
                    ws3.Cells(i, 11).Value = ws2.Cells(i, 11)
                End If

        End Select
    Next j
Next i

End Sub

虽然如果在我的数据库中找不到这个人,我有一个Sub通过列并在单元格上写“TECNICO NAO ENCONTRADO”,如下所示。

enter image description here

由于此值不是我得到的日期:

  

运行时错误13:输入不匹配

在这一行:

ws3.Cells(i, 7).Value = DateAdd("d", 1, ws2.Cells(i, 8).Value)

有没有办法跳过包含“TECNICO NAO ENCONTRADO”的单元格?

1 个答案:

答案 0 :(得分:2)

我想你的意思是改变这一行

ws3.Cells(i, 7).Value = DateAdd("d", 1, ws2.Cells(i, 8).Value)

和类似的如果你有一些,进入:

If IsDate(ws2.Cells(i, 8).Value) Then ws3.Cells(i, 7).Value = DateAdd("d", 1, ws2.Cells(i, 8).Value)