macro if statement

gerson02

New Member
Joined
Sep 9, 2018
Messages
10
Hello again,

i want to create a macro that will compute the rating. when I run the macro and click the button,
I receive Run-time Error '13': Type Mismatch when I try to run the code. Debug highlights the 'IF' statements, but I can't figure out where the mistake is. Any help would be appreciated. Thanks

Private Sub CommandButton1_Click()
Dim score As Integer, result As String
rating = Range("E6:E20").Value

If rating > "98.5%" Then
result = "Exceeds"
ElseIf rating = "96% > 98.49%" Then
result = "Meets"
Else
result = "Needs"
End If

Range("F6:F20").Value = result
End Sub



SCORERATINGRANGERATING
95.00% must be "Needs"98.50% - 100%Exceeds
98.01% must be "Meets"96% - 98.49%Meets
99.00% must be "Exceeds"below 96%Needs
99.50%
97.66%
94.50%
95.78%
98.56%
97.69%
95.45%
98.00%
99.97%
98.99%
97.97%
98.95%

<tbody>
</tbody>
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
The reason for the error is that you assigned all of the values in range E6:E20 to the rating variable. That assignment created a two-dimensional array of values (yes, two-dimensional even though the values come from a single column). In your "If" statement, you are asking if that array equals a text string... that is your type mismatch. You will need to set up a loop and run through and test each array element and assign those individual results to the result variable (which will need to be an array equal in size to the rating array). Also your "ElseIf" comparison is not correct. Try something like this maybe...
Code:
Private Sub CommandButton1_Click()
  Dim R As Long, Rating As Variant, Result As Variant
  Rating = Range("E6:E20").Value
  ReDim Result(1 To UBound(Rating), 1 To 1)
  For R = 1 To UBound(Rating)
    If Rating(R, 1) >= "98.5%" Then
      Result(R, 1) = "Exceeds"
    ElseIf Rating(R, 1) > "96% and Rating(R,1) < 98.5%" Then
      Result(R, 1) = "Meets"
    Else
      Result(R, 1) = "Needs"
    End If
  Next
  Range("F6:F20").Value = Result
End Sub
Note: I am not totally sure (and cannot know without more information), but I don't think your "If" and "ElseIf" tests will work... it kind of depends on what is in the cells you are testing. If the values are numeric percentages, then their value (what you stored in the Result array) will be smaller numbers without the percent symbol, so you would need to test (for the "If" for example) against 0.985, not "98.5%".
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,215
Members
448,554
Latest member
Gleisner2

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