Sub TestConditions()
Range("A1").Select
If IsEmpty(ActiveCell) Then
MsgBox "The cell is empty."
Else
If IsNumeric(ActiveCell.Value) Then
If ActiveCell.Value = 0 Then
ActiveCell.Offset(0, 1).Value ="zero"
ElseIf ActiveCell.Value > 0 Then
ActiveCell.Offset(0, 1).Value ="positive"
ElseIf ActiveCell.Value < 0 Then
ActiveCell.Offset(0, 1).Value ="negative"
End If
Else
ActiveCell.Offset(0, 1).Value = "text"
End If
End If
End Sub
Select Case 測(cè)試表達(dá)式
Case 表達(dá)式1
如果表達(dá)式1匹配測(cè)試表達(dá)式的語(yǔ)句
Case 表達(dá)式2
如果表達(dá)式2匹配測(cè)試表達(dá)式的語(yǔ)句
Case 表達(dá)式N
如果表達(dá)式N匹配測(cè)試表達(dá)式的語(yǔ)句
Case Else
如果沒(méi)有表達(dá)式匹配測(cè)試表達(dá)式要執(zhí)行的語(yǔ)句
End Select
Sub TestButtons()
Dim question As String
Dim bts As Integer
Dim myTitle As String
Dim myButton As Integer
question = "Do you want to open a new workbook?"
bts = vbYesNoCancel + vbQuestion + vbDefaultButton1
myTitle = "New Workbook"
myButton = MsgBox(prompt:=question, buttons:=bts, _ title:=myTitle)
Select Case myButton
Case 6
Workbooks.Add
Case 7
MsgBox "You can open a new book manually later."
Case Else
MsgBox "You pressed Cancel."
End Select
End Sub
Select Case myButton
Case vbYes
Workbooks.Add
Case vbNo
MsgBox "You can open a new book manually later."
Case Else
MsgBox "You pressed Cancel."
End Select
你可以忽略Else子句,可以按下述方法修改一下Select Case語(yǔ)句:
Select Case myButton
Case vbYes
Workbooks.Add
Case vbNo
MsgBox "You can open a new book manually later."
Case vbCancel
MsgBox "You pressed Cancel."
End Select
更多建議: