用于循环跳过数据条目的VBA

时间:2017-06-27 05:59:30

标签: vba excel-vba excel

我使用for循环将数据从一个工作表(data / Sheet1)排序到另一个工作表(calc / Sheet2)。

Sub ToolSort()
Dim data As Worksheet
Set data = ThisWorkbook.Sheets("Sheet1")

Dim calc As Worksheet
Set calc = ThisWorkbook.Sheets("Sheet2")

Dim lrData As Integer
lrData = data.Cells(Rows.count, 1).End(xlUp).Row

For x = 2 To lrData

.......

    'Do not add to calc sheet if column 6 says walter
    If data.Cells(x, 6) = "Walter" Or data.Cells(x, 6) = "walter" Then
        x = x + 1
    End If

    'Put data into columns
    lrcalc = calc.Cells(Rows.count, (col + s)).End(xlUp).Row
    calc.Cells((lrcalc + 1), (col + s)).Value = data.Cells(x, 7)

Next x
End Sub

我想在第6列中跳过任何说Walter的条目,这样它们就不会被添加到calc / Sheet2中,最好的方法是什么?我不认为这是对的,但我不确定从哪里开始

1 个答案:

答案 0 :(得分:1)

只需制作相反的If条件并将代码放在那里:

'If not Walter AND not walter, then add to calc
If data.Cells(x, 6) <> "Walter" And data.Cells(x, 6) <> "walter" Then
    lrcalc = calc.Cells(Rows.count, (col + s)).End(xlUp).Row
    calc.Cells((lrcalc + 1), (col + s)).Value = data.Cells(x, 7)
End If

对于条件A和B:

!(A or B) = !A and !B