Loop for testing

kinny2009

New Member
Joined
Jul 7, 2016
Messages
18
Hi guys,

Just writing a script for testing "J" columns value.

If it's >= 200, then "AV" column show "passed"

But seems like below doesn't work.

Thanks for your help in advance.

Code:
Sub a()

Dim i As Long

For i = 1 To Rows.Count

If Cells(i, "J").Value >= 200 Then

Cells(i, "AV").Value = "passed"

End If

Next i

End Sub
()
 
Assuming row 1 has headers and your data starts in row 2, try the following macro:
Code:
Sub kinny()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("J1:K" & LastRow).AutoFilter Field:=1, Criteria1:=">=200"
    Range("J1:K" & LastRow).AutoFilter Field:=2, Criteria1:="=Yes"
    Range("AV2:AV" & LastRow).SpecialCells(xlCellTypeVisible) = "passed"
    Range("J1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Code:
Sub a()
    Dim i As Long

    With ActiveSheet
        For i = 0 To .UsedRange.Rows.count - 1
            If .Range("F1").Offset(i).Value >= 200 Then
                .Range("AV1").Offset(i).Value = "passed"
            End If
        Next i
    End With
End Sub

or

Code:
Sub a()
    Dim i As Long

    With ActiveSheet
        For i = 0 To .UsedRange.Rows.count - 1
            If .Range("F1").Offset(i).Value >= 200 And .Range("K1").Offset(i).Value = "Yes" Then
                .Range("AV1").Offset(i).Value = "passed"
            End If
        Next i
    End With
End Sub
 
Upvote 0
Which line of code is highlighted when you click "Debug"?
 
Upvote 0
Thanks, rlv01

But seems like all values in Col "AV" turn "passed" in regardless Col "F" & "K"...

Then it sounds like you also have non-numeric data in col J, so add a test for numeric values:

Code:
Sub a()
    Dim i As Long

    With ActiveSheet
        For i = 0 To .UsedRange.Rows.Count - 1
            If IsNumeric(.Range("J1").Offset(i).Value) Then
                If .Range("J1").Offset(i).Value >= 200 And .Range("K1").Offset(i).Value = "Yes" Then
                    .Range(AV1").Offset(i).Value = "Passed"
                End If
            End If
        Next i
    End With
End Sub
 
Upvote 0
Hello Kinny

I tried your code exactly the way you have it in posting #6 , and everything worked perfect. The only way I could get it not to put the word 'passed' in column 'AV', was to have the contents of column 'K' written as 'YES'. You will note this is in upper case letters. The way your 'IF' statement is written, VBA will search 'K' column for something written EXACTLY as you specified, which in your case is 'Yes'. The way to get around this problem is to change your code.

Instead of


Code:
[COLOR=black][FONT=Arial]If Cells(i, "J") >= 200 And Cells(i, "K") = "Yes" Then[/FONT][/COLOR]
change that line to

Code:
[COLOR=black][FONT=Arial]If Cells(i, "J") >= 200 And UCase(Cells(i, "K")) = "YES" Then[/FONT][/COLOR]
Doing this won't actually change anything written in your worksheet, but it changes the way VBA does the comparison. Just a side thought, but if you might have more than 32,767 rows of data, you will need to change the Dim statement to read

Code:
[COLOR=black][FONT=Arial]Dim RowsC as Long[/FONT][/COLOR]

I hope this helps.

TotallyConfused
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top