1、If
If Range("B2").Value >=60 Then Range("C2").Value ="及格" Else Range("C2").Value="不及格"
'如果。。。那么。。。。否则。。。。
If Range("B2").Value >= 60 ThenRange("C2").Value = "及格"
ElseRange("C2").Value = "不及格"
End If
If Range("B2").Value >= 90 ThenRange("C2").Value = "优秀"
ElseIf Range("B2").Value >= 80 ThenRange("C2").Value = "良好"
ElseIf Range("B2").Value >= 60 ThenRange("C2").Value = "及格"
ElseRange("C2").Value = "不及格"
End If
2、Select Case
Select Case Range("B2").ValueCase Is >= 90Range("C2").Value = "优秀"Case Is >= 80Range("C2").Value = "良好"Case Is >= 60Range("C2").Value = "及格"Case ElseRange("C2").Value = "不及格"
End Select
3、For....Next
Sub ShtAdd()Dim i As Byte '定义一个byte类型变量For i = 1 To 5 Step 1Worksheets.Add '在活动工作表前插新工作表Next i
End Sub
Sub ShtAdd()Dim i As Byte '定义一个byte类型变量For i = 1 To 5 Step 1Worksheets.Add '在活动工作表前插新工作表Exit For '退出For循环Next i
End Sub
'方法一
Sub Test()Dim i As ByteDim Irow As ByteIrow = 2For i = 1 To 4 Step 1Select Case Range("B" & Irow).ValueCase Is >= 90Range("C" & Irow).Value = "youxuo"Case Is >= 80Range("C" & Irow).Value = "lianghao"Case Is >= 60Range("C" & Irow).Value = "jigei"Case ElseRange("C" & Irow).Value = "bujigei"End SelectIrow = Irow + 1Next i
End Sub
'方法二
Sub Test()Dim i As ByteFor i = 2 To 5 Step 1Select Case Range("B" & i).ValueCase Is >= 90Range("C" & i).Value = "youxuo"Case Is >= 80Range("C" & i).Value = "lianghao"Case Is >= 60Range("C" & i).Value = "jigei"Case ElseRange("C" & i).Value = "bujigei"End SelectNext i
End Sub
用For Each....Next语句循环处理集合或者数组中的成员
Sub ShtName()Dim sht As Worksheet, i As Integer '定义Worksheet类型变量shti = 1For Each sht In WorksheetsRange("A" & i) = sht.Namei = i + 1Next sht
End Sub
Do While和Do Until(Do While False退出, Do Until True退出)
Sub ShtAdd()Dim i As Bytei = 1Do While i<=5Worksheets.Addi = i+1
End SubSub ShtAdd()Dim i As Bytei = 1Do Worksheets.Addi = i+1Loop While i<=5
End SubSub ShtAdd()Dim i As Bytei = 1Do If i>5 Then Exit DoWorksheets.Addi = i+1Loop
End Sub
GOTO让程序转到指定语句执行
Sub Sum_Test()Dim mysum As Long, i As Integeri = 1x: mysum = mysum + 1i = i + 1if i <= 100 Then GoTo xMsgBox "1到100和:"&mysum
End Sub
With简写代码(对同一对象操作时)
Sub FontSet()Worksheets("Sheet1").Range("A1").Font.Name = '仿宋' '字体Worksheets("Sheet1").Range("A1").Font.size = '12 '字号Worksheets("Sheet1").Range("A1").Font.Bold = True '字体加粗Worksheets("Sheet1").Range("A1").Font.ColorIndex = 3 '红色
End SubSub FontSet()With Worksheets("Sheet1").Range("A1").Font.Name = '仿宋' '字体.size = '12 '字号.Bold = True '字体加粗.ColorIndex = 3 '红色End With
End Sub