Private Sub Command1_Click()
Dim Year As Integer
Dim Name As Integer
Year = InputBox("请输入年份:")
Label1.Caption = Str(Year) + "年的生肖为:"
Name = Year Mod 12
Select Case Name
Case 4
Label2.Caption = "鼠"
Case 5
Label2.Caption = "牛"
Case 6
Label2.Caption = "虎"
Case 7
Label2.Caption = "兔"
Case 8
Label2.Caption = "龙"
Case 9
Label2.Caption = "蛇"
Case 10
Label2.Caption = "马"
Case 11
Label2.Caption = "羊"
Case 0
Label2.Caption = "猴"
Case 1
Label2.Caption = "鸡"
Case 2
Label2.Caption = "狗"
Case 3
Label2.Caption = "猪"
End Select
End Sub
我运行了这段程序,可是inputbox弹出的对话框我一选择“清除”命令就会出现错误
请高手帮忙

解决方案 »

  1.   

    原因是你inputbox上按了取消后就会返回一个"",所以就出现类型转换错误,最快的解决办法是在函数的开始地方加个错误处理就好了啦,On Error Resume Next
    或者用个中间变量
    Dim s As String
    s=inputbox(...)
    if s<>"" then
       year=iif(s = "", 0, val(s))
    end if
      

  2.   


    Private Sub Command1_Click()
    Dim Year As Integer
    Dim Name As Integer
    On Error Resume Next
    Year = InputBox("&Ccedil;&euml;&Ecirc;&auml;&Egrave;&euml;&Auml;ê·&Yacute;&pound;&ordm;")
    If Len(Year) < 4 Then
      MsgBox "&Ccedil;&euml;&Ecirc;&auml;&Egrave;&euml;&Otilde;&yacute;&Egrave;·&micro;&Auml;&Auml;ê·&Yacute;"
      Exit Sub
    End If
    Label1.Caption = str(Year) + "&Auml;ê&micro;&Auml;&Eacute;ú&ETH;¤&Icirc;&ordf;&pound;&ordm;"
    Name = Year Mod 12
    Select Case Name
    Case 4
    Label2.Caption = "&Ecirc;ó"
    Case 5
    Label2.Caption = "&Aring;&pound;"
    Case 6
    Label2.Caption = "&raquo;&cent;"
    Case 7
    Label2.Caption = "&Iacute;&Atilde;"
    Case 8
    Label2.Caption = "&Aacute;ú"
    Case 9
    Label2.Caption = "&Eacute;&szlig;"
    Case 10
    Label2.Caption = "&Acirc;í"
    Case 11
    Label2.Caption = "&Ntilde;ò"
    Case 0
    Label2.Caption = "&ordm;&iuml;"
    Case 1
    Label2.Caption = "&frac14;&brvbar;"
    Case 2
    Label2.Caption = "&sup1;·"
    Case 3
    Label2.Caption = "&Ouml;í"
    End Select
    End Sub
      

  3.   

    Private Sub Command1_Click()
    Dim Year As String
    Const SX As String = "猴鸡狗猪鼠牛虎兔龙蛇马羊"
    Year = InputBox$("请输入年份:")
    If Len(Year) And Val(Year) > 0 Then
    Label1.Caption = Year + "年的生肖为:"
    Label2.Caption = Mid$(SX, Year Mod 12 + 1, 1)
    End If
    End Sub