Sub 判断语句select() Dim i i = 1 Do While i < 4 Select Case Cells(i, 1).Value Case "A" Cells(i, 2) = "优秀" Case "B" Cells(i, 2) = "良好" Case Else Cells(i, 2) = "不及格" End Select i = i + 1 Loop End Sub 在这个例子中,我们根
Select Case i Case 2 To 4: Cells(2, 1).Font.Color = -1677 End Select 这段代码的功能是:当变量i的值在2到4之间时,将单元格A2的字体颜色设置为特定的颜色(此处颜色代码为-1677,代表红色,但在Excel VBA中通常使用RGB函数或ColorIndex来设置颜色,但-1677在某些环境中也可能被识别为红色)。 性能问题:...
可以看到,Select Case结构把 If结构中的条件表达式拆分了,即把变量和判断条件分开了。我们看前一个例子,使用Select Case结构,代码如下:Sub MyCode()Dim i As IntegerFor i = 2 To 10Select Case Cells(i, "B").ValueCase Is >= 85Cells(i, "D") = "优"Case Is >= 75Cells(i, "D") = "良...
我们可以使用`Select Case`语句来实现这一功能。 ### VBA代码示例 ```vba Sub GradeStudents() Dim score As Integer Dim grade As String Dim i As Integer Dim lastRow As Long ' 找到最后一行有数据的单元格 lastRow = Cells(Rows.Count, "A").End(xlUp).Row ' 循环遍历A列的每一个分数 For i ...
'根据产品名称条件设置行Select Case BrandStr Case"Time and Tru 普楦"If Sheets("Sheet1").Cells(Target.Row,11).Comment Is Nothing Then'设置批注Sheets("Sheet1").Cells(Target.Row,11).AddComment"提示:请填写单码或者双码,,如 7-8。"Sheets("Sheet1").Cells(Target.Row,11).Comment.Visible =Fal...
Sub shasha()Select Case Cells(1, "a")Case Is = "AA"MsgBox "有1"Case Is = "BB"MsgBox "有3"Case Is = "cc"MsgBox "有73"Case Else MsgBox "更改"End Select End Sub 123456789
Select Case、Case、End Select:选择语句,用于根据不同的条件执行不同的代码块。 Exit:用于退出当前循环或子过程。 With、End With:用于在代码块中引用对象的属性和方法,避免重复引用对象名称。 Option Explicit:在模块的开头使用,要求显式声明所有变量,以防止拼写错误和未声明的变量。
Private Sub CommandButton4_Click()Dim a(7, 14) As SingleDim i, j As IntegerFor i = 0 To 14For j = 0 To 7a(j, i) = Sheets("可行性").cells(102 + 2 * j, 4 + i).ValueSelect Case a(j, i)Case 1 To 3599a(j, i) = 1Case 3600 To 7799a(j, i) = 2Case...
If Cells(1, i) = "" Then Cells(2, i) = "无数据" Else Cells(2, i) = "有数据" End If Next End Sub 二、Select…Case语句 1、概念介绍 在武侠世界中,有北乔峰和南慕容势均力敌、旗鼓相当。在VBA世界里,也有与“IF…Then…Else”齐名的条件语句“Select… Case”语句。假如遇到这样的条件:“...
Case Else Cells(y, 1).Interior.ColorIndex = 3 End Select Next End Sub 程序分析:首先使用Cells表示单元格,然后用“For…Next”循环语句从第以行到第10行检查Cells(y,1)的值,如果该值为70以上(这种类型的比较要使用Is来写成“Is>=70” ),就执行语句“Cells(y, 1).Interior.ColorIndex = 5”,就行...