Laura
Laura

Reputation: 103

VBA for loop- skipping data entries

I'm sorting data from one sheet (data/Sheet1) into another (calc/Sheet2) using a for loop.

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

I want to skip any entries that say Walter in column 6 so they don't get added to calc/Sheet2, what is the best way to do this? I don't think this is quite right but I'm not sure where to go from here

Upvotes: 1

Views: 49

Answers (1)

Egan Wolf
Egan Wolf

Reputation: 3573

Just make the opposite If condition and put your code there:

'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

For conditions A and B:

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

Upvotes: 1

Related Questions