If statement within For/Next loop

smapple

New Member
Joined
Jan 15, 2018
Messages
6
Hello,

I have a monthly process around a dataset that I'm trying to automate. The data comprises records so that each row is a unique record. I have a calculated field called % Pass Rate and I want to run a simple if statement for every record that says, 'if the score is 85% or more assign 1 otherwise assign 0 in the next column.' The columns are the same each month, but the number of rows will change.

I'm able to calculate the % Pass Rate in Column AE, but when I run the second part of the code, nothing seems to happen (I don't even get an error), whereas I would've expected that Column AF would be a series of 1s and 0s. Can anyone see what I'm missing?

Cheers,


So far I have:

' Calculate % Pass Rate
Dim YesAns As Long
Dim NAAns As Long
Dim LastRow As Long
Dim ThisRow As Long

LastRow = Range("A" & Rows.Count).End(xlUp).Row
For ThisRow = 2 To LastRow
YesAns = Application.WorksheetFunction.CountIf(Sheets("Data").Range(Cells(ThisRow, 6), Cells(ThisRow, 30)), "Yes")
NAAns = Application.WorksheetFunction.CountIf(Sheets("Data").Range(Cells(ThisRow, 6), Cells(ThisRow, 30)), "N/A") Sheets("Data").Cells(ThisRow, 31) = (YesAns + NAAns) / 25
Next

' Determines if file score is above 85% threshold
Dim PassFail As Long
Dim result As Integer

PassFail = Sheets("Data").Range("AE" & Rows.Count).End(xlUp).Row For ThisRow = 2 To LastRow
If PassFail >= 0.85 Then
result = 1
Else
result = 0
Sheets("Data").Cells(ThisRow, 32) = result
End If

Next
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hello,

I have a monthly process around a dataset that I'm trying to automate. The data comprises records so that each row is a unique record. I have a calculated field called % Pass Rate and I want to run a simple if statement for every record that says, 'if the score is 85% or more assign 1 otherwise assign 0 in the next column.' The columns are the same each month, but the number of rows will change.

I'm able to calculate the % Pass Rate in Column AE, but when I run the second part of the code, nothing seems to happen (I don't even get an error), whereas I would've expected that Column AF would be a series of 1s and 0s. Can anyone see what I'm missing?

Cheers,


So far I have:

' Calculate % Pass Rate
Dim YesAns As Long
Dim NAAns As Long
Dim LastRow As Long
Dim ThisRow As Long

LastRow = Range("A" & Rows.Count).End(xlUp).Row
For ThisRow = 2 To LastRow
YesAns = Application.WorksheetFunction.CountIf(Sheets("Data").Range(Cells(ThisRow, 6), Cells(ThisRow, 30)), "Yes")
NAAns = Application.WorksheetFunction.CountIf(Sheets("Data").Range(Cells(ThisRow, 6), Cells(ThisRow, 30)), "N/A") Sheets("Data").Cells(ThisRow, 31) = (YesAns + NAAns) / 25
Next

' Determines if file score is above 85% threshold
Dim PassFail As Long
Dim result As Integer

PassFail = Sheets("Data").Range("AE" & Rows.Count).End(xlUp).Row For ThisRow = 2 To LastRow
If PassFail >= 0.85 Then
result = 1
Else
result = 0
Sheets("Data").Cells(ThisRow, 32) = result
End If

Next
You should be able to replace what I highlighted in red above with this single line of code...
Code:
Range("AF2:AF" & LastRow) = Evaluate("0+(AE2:AE" & LastRow & ">=0.85)")
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,391
Messages
6,124,673
Members
449,179
Latest member
fcarfagna

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