简体   繁体   English

直到循环语法错误vba excel

[英]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我收到错误:运行时错误 438:对象不支持此属性或方法在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只是对@zedfoxus 帖子的一个想法

Single line ifs don't need an end if单行 ifs 不需要结束 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.我已经使用二维数组并利用工作表函数 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:最后,因为Average变量只使用一次,所以它并不真的需要在那里(虽然它可以被认为与MyArr相同,但它太臃肿而无法包含在Vlookup中,它会变得难以阅读),你可以删除它并在 Vlookup 中引用它的构成来进一步压缩代码,最后,我们可以通过使用 for next 循环并轮询到最后一行数据来删除i=3i=i+1 ,如下所示:

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.我不知道你为什么要乘以 100 而我没有你的测试数据。 I made my own test data but had to remove the *100 to make it work, my data was in column F.我制作了自己的测试数据,但必须删除 *100 才能使其工作,我的数据在 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.我想知道您是否想使用公式而不是 VBA。

Vlookup can do this. Vlookup 可以做到这一点。 As below, hope this help.如下,希望这有帮助。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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