vba编码“;直到或在某个时候做”;及;“循环”;
我的excel文件中有3列:名称、百分比和等级 我想自动生成字母等级(例如,95%将生成“A”),以填充第三列直到最后一行 我不断收到一个与我如何循环此代码有关的错误。有什么见解吗vba编码“;直到或在某个时候做”;及;“循环”;,vba,loops,Vba,Loops,我的excel文件中有3列:名称、百分比和等级 我想自动生成字母等级(例如,95%将生成“A”),以填充第三列直到最后一行 我不断收到一个与我如何循环此代码有关的错误。有什么见解吗 Sub Grades() Dim score As Integer Dim x As Integer x = 1 score = Sheets("sheet1").Cells(x, 2).Value Do While score <> "" I
Sub Grades()
Dim score As Integer
Dim x As Integer
x = 1
score = Sheets("sheet1").Cells(x, 2).Value
Do While score <> ""
If score >= 90 And score <= 100 Then
Sheets("Sheet1").Cells(x, 3).Value = "A"
ElseIf score > 79 And score < 90 Then
Sheets("Sheet1").Cells(x, 3).Value = "B"
ElseIf score > 69 And score < 80 Then
Sheets("Sheet1").Cells(x, 3).Value = "C"
ElseIf score > 59 And score < 70 Then
Sheets("Sheet1").Cells(x, 3).Value = "D"
ElseIf score < 60 Then
Sheets("Sheet1").Cells(x, 3).Value = "F"
Else
Sheets("Sheet1").Cells(x, 3).Value = ""
End If
x = x + 1
score = Sheets("sheet1").Cells(x, 2).Value
Loop
subgrades()
将分数设置为整数
作为整数的Dim x
x=1
分数=表(“表1”)。单元格(x,2)。值
“边做边得分”
如果得分>=90,得分79,得分<90,则
表(“表1”)。单元格(x,3)。Value=“B”
ElseIf评分>69,然后评分<80
表(“表1”)。单元格(x,3)。Value=“C”
ElseIf得分>59,然后得分<70
表(“表1”)。单元格(x,3)。Value=“D”
ElseIf评分<60分
表(“表1”)。单元格(x,3)。Value=“F”
其他的
表(“表1”).单元格(x,3).Value=“”
如果结束
x=x+1
分数=表(“表1”)。单元格(x,2)。值
环
问题在于分数是一个数字,并且始终为“”
子示例2()
将分数设置为整数
作为整数的Dim x
x=2
附页(“第1页”)
执行While.Cells(x,2).Value“”
分数=.Cells(x,2).Value
如果得分>=90,得分79,得分<90,则
.Cells(x,3).Value=“B”
ElseIf评分>69,然后评分<80
.Cells(x,3).Value=“C”
ElseIf得分>59,然后得分<70
.Cells(x,3).Value=“D”
ElseIf评分<60分
.Cells(x,3).Value=“F”
其他的
.Cells(x,3).Value=“”
如果结束
x=x+1
环
以
端接头
问题在于分数是一个数字,并且始终为“”
子示例2()
将分数设置为整数
作为整数的Dim x
x=2
附页(“第1页”)
执行While.Cells(x,2).Value“”
分数=.Cells(x,2).Value
如果得分>=90,得分79,得分<90,则
.Cells(x,3).Value=“B”
ElseIf评分>69,然后评分<80
.Cells(x,3).Value=“C”
ElseIf得分>59,然后得分<70
.Cells(x,3).Value=“D”
ElseIf评分<60分
.Cells(x,3).Value=“F”
其他的
.Cells(x,3).Value=“”
如果结束
x=x+1
环
以
端接头
Petrie Manuel,我不确定您想在什么情况下使用您的Else
声明:
Sheets("Sheet1").Cells(x, 3).Value = ""
因为如果分数低于60分,你会把aF,不是吗
无论如何,为了简化代码和逻辑语句,我使用了选择Case
Sub Grades()
Dim score As Integer
Dim x As Integer
Dim sht1 As Worksheet
Set sht1 = ThisWorkbook.Worksheets("sheet1")
' if your first row is for table headers
x = 2
With sht1
Do While .Cells(x, 2).Value <> ""
score = .Cells(x, 2).Value
Select Case score
Case Is >= 90
.Cells(x, 3).Value = "A"
Case Is >= 80
.Cells(x, 3).Value = "B"
Case Is >= 70
.Cells(x, 3).Value = "C"
Case Is >= 60
.Cells(x, 3).Value = "D"
Case Is < 60
.Cells(x, 3).Value = "F"
Case Else
.Cells(x, 3).Value = ""
End Select
x = x + 1
Loop
End With
End Sub
subgrades()
将分数设置为整数
作为整数的Dim x
Dim sht1作为工作表
设置sht1=此工作簿。工作表(“表1”)
'如果第一行是表格标题
x=2
使用sht1
执行While.Cells(x,2).Value“”
分数=.Cells(x,2).Value
选择案例分数
案例>=90
.Cells(x,3).Value=“A”
案例>=80
.Cells(x,3).Value=“B”
案例>=70
.Cells(x,3).Value=“C”
案例>=60
.Cells(x,3).Value=“D”
例<60
.Cells(x,3).Value=“F”
其他情况
.Cells(x,3).Value=“”
结束选择
x=x+1
环
以
端接头
Petrie Manuel,我不确定您想在什么情况下使用您的Else
声明:
Sheets("Sheet1").Cells(x, 3).Value = ""
因为如果分数低于60分,你会把aF,不是吗
无论如何,为了简化代码和逻辑语句,我使用了选择Case
Sub Grades()
Dim score As Integer
Dim x As Integer
Dim sht1 As Worksheet
Set sht1 = ThisWorkbook.Worksheets("sheet1")
' if your first row is for table headers
x = 2
With sht1
Do While .Cells(x, 2).Value <> ""
score = .Cells(x, 2).Value
Select Case score
Case Is >= 90
.Cells(x, 3).Value = "A"
Case Is >= 80
.Cells(x, 3).Value = "B"
Case Is >= 70
.Cells(x, 3).Value = "C"
Case Is >= 60
.Cells(x, 3).Value = "D"
Case Is < 60
.Cells(x, 3).Value = "F"
Case Else
.Cells(x, 3).Value = ""
End Select
x = x + 1
Loop
End With
End Sub
subgrades()
将分数设置为整数
作为整数的Dim x
Dim sht1作为工作表
设置sht1=此工作簿。工作表(“表1”)
'如果第一行是表格标题
x=2
使用sht1
执行While.Cells(x,2).Value“”
分数=.Cells(x,2).Value
选择案例分数
案例>=90
.Cells(x,3).Value=“A”
案例>=80
.Cells(x,3).Value=“B”
案例>=70
.Cells(x,3).Value=“C”
案例>=60
.Cells(x,3).Value=“D”
例<60
.Cells(x,3).Value=“F”
其他情况
.Cells(x,3).Value=“”
结束选择
x=x+1
环
以
端接头
自:
- “B”列中的数字是百分比->始终在0和100之间
- 您需要将它们解析为10的倍数
Option Explicit
Sub Grades()
Dim cell As range
With Sheets("scores") '<--| change "scores" with your actual sheet name
For Each cell In .range("B1", .Cells(.Rows.Count, "B").End(xlUp)).SpecialCells(xlCellTypeConstants, xlNumbers) '<--| loop through "numeric" column "B" cells down to last non empty one only
cell.Offset(, 1) = Choose(Int(cell.value / 10) + 1, "F", "F", "F", "F", "F", "F", "D", "C", "B", "A", "A")
Next cell
End With
End Sub
选项显式
路基()
暗淡单元格作为范围
由于:
- “B”列中的数字是百分比->始终在0和100之间
- 您需要将它们解析为10的倍数
然后你可以这样做:
Option Explicit
Sub Grades()
Dim cell As range
With Sheets("scores") '<--| change "scores" with your actual sheet name
For Each cell In .range("B1", .Cells(.Rows.Count, "B").End(xlUp)).SpecialCells(xlCellTypeConstants, xlNumbers) '<--| loop through "numeric" column "B" cells down to last non empty one only
cell.Offset(, 1) = Choose(Int(cell.value / 10) + 1, "F", "F", "F", "F", "F", "F", "D", "C", "B", "A", "A")
Next cell
End With
End Sub
选项显式
路基()
暗淡单元格作为范围
带表格(“分数”)“你犯了什么错误?一个Select Case
会更好。你犯了什么错误?一个Select Case
会更好。@PetrieManuel:你成功了吗?@PetrielManuel:你能把Feedbacks给那些尝试帮助的人真是太好了you@PetrieManuel:你挺过来了吗?@PetrielManuel:你能来真是太好了给那些试图帮助你的人提供反馈