《用vba程序设计判断闰年》由会员分享,可在线阅读,更多相关《用vba程序设计判断闰年(3页珍藏版)》请在金锄头文库上搜索。
1、给定一个年份和月份,判断它的季节和这个月的天数。其中 3-5 月为春季,6-8 月为夏 季,9-11 为秋季,12,1,2 月为冬季。二月的天数与闰年有关,判断闰年的条件是:能被 4 整除且不能被 100 整除,或者能被 400 整除。 解:按程序设计步骤: 1.根据题意设计界面,如图 8-19 所示。在这里主要拖放控件摆放位置和大小。 2.修改控件属性,如图 8-20 所示。 3.编写代码。 代码如下: Private Sub CommandButton1_Click() Dim flag As Boolean判断是否输入月份 If Val(TextBox1.Text) = 100 And
2、Val(TextBox2.Text) = 1 And Val(TextBox2.Text) Int(Val(Val(TextBox1.Text) / 100) Or Val(TextBox1.Text) / 400 = Int(Val(Val(TextBox1.Text) / 400) Then是闰年TextBox3.Text = TextBox1.Text + “年是闰年!“ 设置闰年标志 flag = True Else不是闰年TextBox3.Text = TextBox1.Text + “年不是闰年!“flag = False End If判断每月天数 Select Case Val(
3、TextBox2.Text)Case 1, 3, 5, 7, 8, 10, 12TextBox4.Text = TextBox2.Text + “月是 31 天!“Case 2If flag ThenTextBox4.Text = TextBox2.Text + “月是 29 天!“ElseTextBox4.Text = TextBox2.Text + “月是 28 天!“End IfCase 4, 6, 9, 11TextBox4.Text = TextBox2.Text + “月是 30 天!“End Select判断季节Select Case Val(TextBox2.Text)Case
4、 3, 4, 5TextBox5.Text = TextBox1.Text + “年“ + TextBox2.Text + “月是春季!“Case 6, 7, 8TextBox5.Text = TextBox1.Text + “年“ + TextBox2.Text + “月是夏季!“Case 9, 10, 11TextBox5.Text = TextBox1.Text + “年“ + TextBox2.Text + “月是秋季!“Case 1, 2, 12TextBox5.Text = TextBox1.Text + “年“ + TextBox2.Text + “月是冬季!“End SelectElse 报错 MsgBox “数据有错!重新输入“ End IfEnd SubPrivate Sub CommandButton2_Click() 清除 TextBox1.Text = “ TextBox2.Text = “ TextBox3.Text = “ TextBox4.Text = “ TextBox5.Text = “ End Sub 4.运行与调试。如图 8-21 所示。 5.保存程序。图 8-19 设计界面图 8-20 修改属性图 8-21 程序运行界面