应用思考-教育技术论坛

标题: 使用VBA判断平闰年 [打印本页]

作者: etthink    时间: 2018-12-21 08:46
标题: 使用VBA判断平闰年
示例代码
  1. Sub 判断闰年()
  2. Dim year As Integer '用于保存输入的年份
  3. year = CInt(InputBox("请输入需要判断的年份:", "判断闰年")) '输入年份
  4. If year Mod 4 = 0 And year Mod 100 <> 0 Then
  5.   MsgBox "" & "是一个闰年", vbOKOnly, "判断闰年"
  6. Else
  7.     If year Mod 100 = 0 And year Mod 400 = 0 Then
  8.         MsgBox "" & "是一个闰年", vbOKOnly, "判断闰年"
  9.     Else
  10.         MsgBox "" & "不是一个闰年", vbOKOnly, "判断闰年"
  11.     End If
  12. End If
  13. End Sub
复制代码
如果在PPT中使用控件比如文本框来生成或输入年份,修改为:
  1. Dim year As Integer

  2. Private Sub CommandButton1_Click()
  3. year = Rnd * 1000 + 2000
  4. TextBox1.Text = year
  5. End Sub
  6. Private Sub CommandButton2_Click()
  7. year = Int(TextBox1.Text)
  8. If year Mod 4 = 0 And year Mod 100 <> 0 Then
  9.     MsgBox (TextBox1.Text + "年" + "是闰年")
  10.     Else
  11.     If year Mod 100 = 0 And year Mod 400 = 0 Then
  12.     MsgBox (TextBox1.Text + "年" + "是闰年")
  13.     Else
  14.     MsgBox (TextBox1.Text + "年" + "不是闰年")
  15.     End If
  16. End If

  17. End Sub
复制代码
使用时一定要注意自已添加的控件名和事件名。
源文件下载: (, 下载次数: 45)






欢迎光临 应用思考-教育技术论坛 (http://www.etthink.com/) Powered by Discuz! X3.4