VBA程序设计范例.doc
《VBA程序设计范例.doc》由会员分享,可在线阅读,更多相关《VBA程序设计范例.doc(12页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、高考VBA程序设计解析一、累加和问题:1、基本问题问题:编程计算1+2+3+100;程序界面:源代码:Private Sub Command3_Click()Dim s As IntegerDim i As Integers = 0For i = 1 To 100 Step 1 s = s + iNext iText1.SetFocusText1.Value = sEnd SubPrivate Sub Command4_Click()DoCmd.Close acForm, VBA程序设计范例1End Sub2、问题拓展拓展1:编程计算1+3+5+7+99;源代码修改:For i = 1 To
2、99 Step 2 s = s + iNext i拓展2:编程计算2+4+6+8+100;源代码修改:For i = 2 To 100 Step 2 s = s + iNext i拓展3:编程计算1+2+3+N;程序界面:源代码:Private Sub Command10_Click()Dim i As IntegerDim s As IntegerText3.SetFocusIf Trim(Text3.Text) = Space(0) Then MsgBox 请输入N的值!, vbOKOnly Exit SubElseIf Val(Text3.Text) 1 Then MsgBox 请输入大
3、于1的正整数!, vbOKOnly Exit SubEnd Ifs = 0For i = 1 To Val(Text3.Value) Step 1 s = s + iNext iText8.SetFocusText8.Value = sEnd Sub拓展4:编程计算1+(1+2)+(1+2+3)+(1+2+3+4)+(1+2+3+4+ +100);程序界面:源代码:Private Sub Command3_Click()Dim i As Integer, j As IntegerDim s1 As Integer, s2 As Doubles1 = 0s2 = 0For i = 1 To 10
4、0 Step 1 s1 = 0 For j = 1 To i s1 = s1 + j Next j s2 = s2 + s1Next iText1.SetFocusText1.Text = s2End Sub拓展5:编程计算1+(1+3)+(1+3+5)+(1+3+5+7)+(1+3+5+7+ +99);源代码:Private Sub Command3_Click()Dim i As Integer, j As IntegerDim s1 As Integer, s2 As Doubles1 = 0s2 = 0For i = 1 To 50 Step 1 s1 = 0 For j = 1 To
5、 i*2-1 step 2 s1 = s1 + j Next j s2 = s2 + s1Next iText1.SetFocusText1.Text = s2End Sub3、算法相关问题编程计算1*2*3* *100;提示:内存变量的数据类型的确定;二、排序问题1、基本问题问题:提示用户输入两个数,比较其大小;源代码:Private Sub Command4_Click()Dim a1 As StringDim a2 As StringDim b1 As IntegerDim b2 As IntegerText0.SetFocusa1 = Trim(Text0.Text)Text2.Set
6、Focusa2 = Trim(Text2.Text)If a1 = Space(0) Or a2 = Space(0) Then MsgBox 请输入两个待比较的数!, vbOKOnly Exit SubEnd Ifb1 = Val(a1)b2 = Val(a2)Text8.SetFocusIf b1 maxvalue Then maxvalue = arr(i) End If If arr(i) arr(j) Then temp = arr(i) arr(i) = arr(j) arr(j) = temp End If Next jNext iText2.SetFocusFor i = 1
7、To 10 Text2.Text = Text2.Text & arr(i) & ,Next iEnd Sub拓展3:输入一组数, 请按升序排序输出这组数;程序界面:源代码:Private Sub Command6_Click()Dim arr(1 To 10) As IntegerDim i As IntegerDim j As IntegerDim temp As IntegerDim s1 As StringFor i = 1 To 10 arr(i) = InputBox(请输入第 & i & 个数:) s1 = s1 & arr(i) & ,Next iText1.SetFocusT
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VBA 程序设计 范例
限制150内