简体   繁体   中英

Do Until loop Syntax error vba excel

I'm trying to write this module to compute a letter grade from a % in the next cell over and loop through the rows until the row is empty. Whats wrong with the syntax of this code? I get error: Runtime error 438: Object doesnt support this property or method at Average = Cells(i, 6).Valve

Sub Grade()
Dim Average As Double
Dim i As Integer

i = 3

Do Until IsEmpty(Cells(i, 6))

Average = Cells(i, 6).Valve
Average = Average * 100

If (Average <= 60) Then
    Cells(i, 7).Valve = ("E")
End If
If (Average <= 70) Then
    Cells(i, 7).Valve = ("D")
End If
If (Average <= 80) Then
    Cells(i, 7).Valve = ("C")
End If
If (Average <= 90) Then
    Cells(i, 7).Valve = ("B")
End If
If (Average <= 100) Then
    Cells(i, 7).Valve = ("A")
End If

i = i + 1
Loop

End Sub

Change

Dim Average As Double
i As Integer

to

Dim Average As Double
Dim i As Integer

or

Dim Average As Double, i As Integer

or

Dim Average As Double, _
i As Integer

Your code needs a little more work. Use something like this:

Sub Grade()
Dim Average As Double
Dim i As Integer

i = 3

Do Until IsEmpty(Cells(i, 7))

    Cells(i, 6).Value = Average
    ' Perhaps the above should be 
    ' Average = Cells(i,6).Value

    If (Average < 60) Then
        Cells(i, 7).Valve = ("E")
    End If
    If (Average < 70) Then
        Cells(i, 7).Valve = ("D")
    End If
    If (Average < 80) Then
        Cells(i, 7).Valve = ("C")
    End If
    If (Average < 90) Then
        Cells(i, 7).Valve = ("B")
    End If
    If (Average < 100) Then
        Cells(i, 7).Valve = ("A")
    End If

    i = i + 1
Loop

End Sub

Just a thought on @zedfoxus post

Single line ifs don't need an end if

Sub Grade()
Dim Average As Double
Dim i As Long

i = 3

Do Until IsEmpty(Cells(i, 7))

    Average = Cells(i, 6).Value
    Average = Average * 100    
    If (Average < 60) Then Cells(i, 7).Value = ("E")
    If (Average < 70) Then Cells(i, 7).Value = ("D")
    If (Average < 80) Then Cells(i, 7).Value = ("C")
    If (Average < 90) Then Cells(i, 7).Value = ("B")
    If (Average < 100) Then Cells(i, 7).Value = ("A")

    i = i + 1
Loop

End Sub

Further to this though, here is my take on the problem. I have put together a condensed routine using a 2 dimensional array and taking advantage of the worksheet function Vlookup. This works because it will find the closest thing (useful when you are using ranges of numbers)

Sub Grade()
Dim Average As Double, i As Long, MyArr As Variant
MyArr = Array(Array(60, "E"), Array(70, "D"), Array(80, "C"), Array(90, "B"), Array(100, "A"))
i = 3
Do Until IsEmpty(Cells(i, 7))
    Average = Cells(i, 6).Value * 100 'Why * 100? Anyway just copied what you have done in your code
    Cells(i, 7).Value = Application.WorksheetFunction.VLookup(Average, MyArr, 2)
    i = i + 1
Loop
End Sub

And lastly, because the Average variable is only used once, it doesn't really need to be there (whilst it could be argued the same for MyArr it would be too bloated to include in the Vlookup, it would become hard to read), you can remove it and just reference its makeup in the Vlookup to condense the code further, and finally, we can remove i=3 and i=i+1 by using a for next loop and polling to the last row of data like so:

Sub Grade()
Dim i as long, MyArr As Variant
MyArr = Array(Array(60, "E"), Array(70, "D"), Array(80, "C"), Array(90, "B"), Array(100, "A"))
For i = 3 To Range("G" & Rows.Count).End(xlUp).Row
    Cells(i, 7).Value = Application.WorksheetFunction.VLookup(Cells(i, 6).Value * 100, MyArr, 2)
Loop
End Sub

I am not sure why you are multiplying by 100 and I don't have your test data. I made my own test data but had to remove the *100 to make it work, my data was in column F.

40  
50  
60  E
65  E
70  D
75  D
80  C
85  C
90  B
95  B
100 A

This is the code I used:

Sub Grade2()
Dim i As Long, MyArr As Variant
MyArr = Array(Array(60, "E"), Array(70, "D"), Array(80, "C"), Array(90, "B"), Array(100, "A"))
For i = 3 To Range("F" & Rows.Count).End(xlUp).Row
    Cells(i, 7).Value = Application.WorksheetFunction.VLookup(Cells(i, 6).Value, MyArr, 2)
Next
End Sub

I wonder if you want to use Formula instead of VBA.

Vlookup can do this. As below, hope this help.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM