Logical operators work in columns

The Student

New Member
Joined
Sep 27, 2018
Messages
24
Office Version
  1. 365
Platform
  1. Windows
I found this script and i now wonder how to make it work in more than one row. i tried to add Range("A1:A10"). but it gives me a run time-error 13 . Hope you can help.

Sub test()
Dim score1 As Integer, score2 As Integer, result As String


score1 = Range("A1").Value
score2 = Range("B1").Value

If score1 >= 60 And score2 > 1 Then
result = "pass"
Else
result = "fail"
End If

Range("C1").Value = result

End sub()
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Code:
Sub test()
Dim score1 As Integer, score2 As Integer, result As String
Dim rng As Range, cll As Range 

Set rng = Range("A1:A10")

For Each cll in rng.Cells

score1 = cll.Value
score2 = cll.Offset(0, 1).Value

If score1 >= 60 And score2 > 1 Then
result = "pass"
Else
result = "fail"
End If

cll.Offset(0, 2) = result

Next cll

End sub()

You should be able to modify the range to reference any cells you'd like.
 
Upvote 0
Another option
Code:
Sub CheckScore()
   With Range("C1", Range("A" & Rows.Count).End(xlUp).Offset(, 2))
      .Value = Evaluate(Replace(Replace("if((@a>=60)*(@b>1),""Pass"",""fail"")", "@a", .Offset(, -2).Address), "@b", .Offset(, -1).Address))
   End With
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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