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



[TABLE="width: 484"]
<tbody>[TR]
[TD]SCORE[/TD]
[TD]RATING[/TD]
[TD][/TD]
[TD]RANGE[/TD]
[TD]RATING[/TD]
[/TR]
[TR]
[TD]95.00%[/TD]
[TD] must be "Needs"[/TD]
[TD][/TD]
[TD]98.50% - 100%[/TD]
[TD]Exceeds[/TD]
[/TR]
[TR]
[TD]98.01%[/TD]
[TD] must be "Meets"[/TD]
[TD][/TD]
[TD]96% - 98.49%[/TD]
[TD]Meets[/TD]
[/TR]
[TR]
[TD]99.00%[/TD]
[TD] must be "Exceeds"[/TD]
[TD][/TD]
[TD]below 96%[/TD]
[TD]Needs[/TD]
[/TR]
[TR]
[TD]99.50%[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]97.66%[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]94.50%[/TD]
[TD][/TD]
[TD][/TD]
[TD="colspan: 2"][/TD]
[/TR]
[TR]
[TD]95.78%[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]98.56%[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]97.69%[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]95.45%[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]98.00%[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]99.97%[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]98.99%[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]97.97%[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]98.95%[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
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,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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