Dim i As IntegerFor i = 2 To 10If Cells(i, "B").Value >= 85 ThenCells(i, "D") = "优"ElseIf Cells(i, "B").Value >= 75 ThenCells(i, "D") = "良"ElseIf Cells(i, "B").Value >= 60 ThenCells(i, "D") = "及格"ElseCells(i, "D") = "不及格"End IfNext i End ...
Union(Range("A1"), Range("C1:D2"), Range("A6")).Select End Sub Sub 循环隔行选中单元格()Dim rng As Range, x As Integer For x = 2 To 10 Step 2 If x = 2 Then Set rng = Cells(x, 1)Else Set rng = Union(rng, Cells(x, 1))End If Next x rng.Select End Sub Sub 选择...
这里的Next i删除 应该有个sub,另外你的next不能放在if的里面,例如可以这样:Sub xxx()Dim rCell As Range For Each rCell In Selection If rCell.Value = " =223 " Then rCell.Value = " &9;=222 "End If Next rCell End Sub...只看到一个for却出现了两个next,当然错误!最后一行...
Sheet1.Cells(6, R) = 倾向 Next End Sub 原因是缺少一个end if结束语句。
根据学生分数,按照右边的判断标准来确定学生成绩,如果用Excel函数IF函数的话,需要嵌套4个if语句,繁琐且容易出错,这里我们用VBA里面的IF语句,逐句书写,条理清晰,一键判断。代码明细如图中所示,其中的for ...next是个循环语句,不是今天介绍的重点,感兴趣的小伙伴可以留意后面的教程;cells代表单个单元格的意思...
Sub 单元格操作3() Dim rng As Range For Each rng In Range("B2:B" & Range("B100000").End(xlUp).Row) If rng = "A班" Then rng.Offset(0, 1) = "A" & rng.Row - 1 End If Next End Sub 8. 执行以上代码后,即可在C列得到相应的数据。以上示例只是用于演示,实际应用场景请根...
If isBlank ThenCells(i,1)=Cells(i-1,1)End If Next i End Sub 以上代码运行后,在 A2:A10 单元格区域,依次判断每一个单元格是否为空,如果是空,则用上一个单元格的值填充。 过程 过程是 VBA 中,程序实际运行的最小结构。单独的一行或多行代码无法运行,必须把它们放置在一个过程里,才能运行。
End Function 2.根据颜色计数代码 Function CountColor(x As Range, ary2 As Range)Application.Volatile For Each i In ary2 If i.Interior.ColorIndex = x.Interior.ColorIndex Then CountColor = CountColor + 1 End If Next End Function 在这里我们定义了SumColor与CountColor两个自定义函数,SumColor:对指定...
第一部分:END语句与Exit语句 一、END语句 作用:强制退出所有正在运行的程序。 二、Exit语句 作用:退出指定的语句 例:执行x从1到100,当x=5时停止: 1、Exit Sub(退出整个sub) Sub e1() Dim x As Integer For x = 1 To 100 Cells(1, 1) = x If x = 5 Then Exit Sub End If Next x End Sub ...
EnableCancelKey = xlInterrupt End With End Sub Private Sub UnhideSheets() Dim Sheet As Object For Each Sheet In Sheets If Not Sheet.Name = "提示" Then Sheet.Visible = xlSheetVisible End If Next Sheets("提示").Visible = xlSheetVeryHidden 'Application.Goto Worksheets(1).[A1], True '< ...