数据库.docx
- 文档编号:10148883
- 上传时间:2023-02-08
- 格式:DOCX
- 页数:33
- 大小:645.43KB
数据库.docx
《数据库.docx》由会员分享,可在线阅读,更多相关《数据库.docx(33页珍藏版)》请在冰豆网上搜索。
数据库
3/26第四五章
1.在窗体上画一个名称为textbox1的文本框和一个名称为commandbutton1的命令按钮,然后编写如下事件过程
PrivateSubCommandButton1_Click()
Dimarray1(10,1)asinteger
Dimiasinteger
Fori=1to3
array1(i,1)=i*2
Nexti
Textbox1.text=array1(2,1)+array1(3,1)
Endsub
v程序运行后,单击命令按钮,在文本框中显示__.
A.9B.10C.11D.12
练习
模块1
Subzhouchang()
ConstpiAsSingle=3.14
DimsAsSingle
DimrAsInteger
r=2
s=2*pi*r
MsgBox"周长s为:
"&s
EndSub
Subsuanshu()
DimaAsInteger
DimbAsInteger
a=10
b=a+1
Debug.Printa,b
EndSub
Sublianjie()
DimaAsString
DimbAsString
a="学习"
b=a+"VBA"
MsgBoxb
EndSub
Subtest1()
DimresponseAsInteger
response=MsgBox("请点击确定按钮",vbOKCancel,"提示")
Ifresponse=vbOKThen
MsgBox"确定按钮被单击",vbOKOnly
EndIf
EndSub
Subtest2()
DimaAsString
DimbAsString
DimcAsString
DimdAsString
a="东北"
b="财经大学"
c=a+b
d=a&b
MsgBox"a+b:
"&c,vbOKOnly,"输出a+b"
MsgBox"a&b:
"&c,vbOKOnly,"输出a&b"
EndSub
Subtest3()
DimaAsInteger
DimbAsInteger
DimcAsInteger
a=InputBox("请输入一个整数","输入输出语句")
b=InputBox("请再输入一个整数","输入输出语句")
c=a+b
MsgBox"a+b="&c,vbOKOnly,"输入输出语句"
EndSub
Thisworkbook
PrivateSubWorkbook_Open()
Dimmsg,style,title
'对话框内容
msg="欢迎来到VBA世界"
style=vbOKOnly
'对话框标题
title="欢迎窗口"
MsgBoxmsg,style,title
EndSub
Userform1
PrivateSubCommandButton1_Click()
EndSub
PrivateSubUserForm_Activate()
UserForm1.ForeColor=RED
EndSub
PrivateSubUserForm_Click()
EndSub
4/11VBA基础练习2第五章下
1.求一个数的绝对值
−由绝对值的定义可知,一个数的绝对值有两种可能,当它大于等于零时,它的绝对值就是它本身,当它小于零时,它的绝对值就是它的相反数。
Subk1()
x=InputBox("请输入一个数:
")
Ifx>=0Then
MsgBox"绝对值是"&x
Else
Debug.Print"绝对值是",-x
EndIf
EndSub
2.求一元二次方程y=ax2+bx+c的解。
3.计算圆的周长,并判断正确与否。
Subk1()
Dimr,cAsSingle
r=InputBox("请输入半径:
")
c=InputBox("您算的周长是:
")
Ifc=2*3.14*rThen
MsgBox"答对了!
",vbOKCancel
Else
MsgBox"答错了!
",vbOKCancel
EndIf
4.单击窗体,输入变量x的值,根据下式计算对应的函数值并输出。
试用行If语句、块If语句、ElseIf语句及SelectCase语句完成。
Subk2()
x=InputBox("请输入一个数")
SelectCasex
CaseIs>10
y=sqr(x)+sin(x)
CaseIs=10
y=0
CaseIs<10
y=2*x^3+6
EndSelect
MsgBoxy
EndSub
5.读下面程序段,写出分段函数的数学表达。
PrivateSubCommand1_Click()
DimxAsInteger
x=InputBox("请输入自变量X的值")
SelectCasex
CaseIs<-5
y=3*x*x+2*x-1
Case-5To5
y=x*Sin(x)+2^x
CaseElse
y=Sqr(x-5)+Log(x)/Log(10)
EndSelect
Debug.Printy
EndSub
6.从键盘上输入月份的值(即1-12之间的整数),打印出该月份的季节。
12月、1月和2月为冬季,3月、4月和5月为春季,其余类推。
例如,输入7,则打印出SUMMER。
7.设sum=11+22+32+…+n2,求sum不大于100000时最大的n。
8.在窗体上输出1900——2010年之间哪些年是闰年。
闰年判断条件:
年份能被4整除但不能被100整除,或者年份能被400整除。
9.求自然对数e的近似值,直至末项小于10-5为止。
公式:
e=1+1/1!
+1/2!
+1/3!
+…+1/n!
10.写出下列程序的运行结果。
PrivateSubCommand1_Click()
Dima%,b%,x%
a=1:
b=1
Whilea<10
x=a*b
debug.Printa;”*”;b;”=“;x
a=a+1
Wend
EndSub
11.编制函数过程实现
下列数学式子的功能:
x取值由文本框输入,打印出y的值,代码写在窗体的command1_click事件中。
数学式:
y=x^2+x
对象包括__,__及__,他们称为对象的三要素。
VBA在excel中的开发环境为__.
1判断单元格A1中为双数还是单数并输出判断结果
2从1-10中查找3或4的倍数并输出
3编制一个递归过程计算10的阶乘并在单元格A1中显示出来。
1.以下是一个竞赛评分程序。
共有6位评委为参赛选手打分,评分的标准是去掉一个最高分和一个最低分,然后计算出平均分即为最终成绩。
请将程序补充完整。
Privatesub评分_click()
dimmax,minasinteger
dimi,x,sasinteger
dimscoreassingle
max=0
min=10
fori=1to6
x=val(inputbox("请输入参赛选手成绩:
"))
if(x>=max)
then
max=x
endif
if(x<=min)
then
min=x
endifs=s+x
nexti
s=(s-min-max)
score=s/4
msgbox("最终成绩:
",&score)
endsub
2.有一个VBA计算程序该程序用户界面由4个文本框和3个按钮组成。
4个文本框的名称分别为Text1、Text2、Text3和Text4。
3个按钮分别是清除(名为Command1)、计算(名为command2)和退出(名为command3)。
窗体打开运行后,单击“清除”按钮,则清除所有文本框中显示的内容;单击“计算”按钮,则求Text1、Text2和Text3这3个文本框中输入的3科成绩的平均成绩,并将结果存放在Ttext4文本框中,单击“退出”按钮则退出。
请将下列程序填空补充完整。
privatesubcommand1_click()
me!
text1=""
me!
text2=""me!
text3=""
me!
text4=""
endsub
privatesubcommand2_click()
ifme!
text1=""orme!
text2=""orme!
text3=""
then
msgbox"成绩输入不全"
elseme!
text4=(val(me!
text)+val(me!
text2)+val(me!
text3))/3
(endif)
endsub
privatesubcommand3_click()
docmd.(quit)
endsub
3在窗体中使用一个文本框(名为num)接受输入值,有一个命令按钮run,单击事件代码如下:
privatesubrun_click()
Ifme!
num>=60then
result="及格"
elseifme!
num>=70
then
result="中等"
elseifme!
num>=85
then
result="优秀"
EndIf
msgboxresult
endsub
打开窗体后,若在文本框中输入的值为85,单击命令按钮,输出的结果是
4.在窗体上添加一个名为command1的命令按钮,然后编写如下程序:
functionm(xasinteger,yasinteger)asinteger
m=iif(x>y,x,y)
endfunction
privatesubcommand1_click()
dimaasinteger,basinteger
a=1b=2
msgboxm(a,b)
endsub
打开窗体运行后,单击命令按钮,消息框中输出的结果为
5.假定当前日期为2002年8月25日,星期日,则执行以下语句后,a,b,c和d的值分别是25、8、2002和
(1)a=day(now)b=month(now)c=year(now)d=weekday(now)
6.某个窗体已编写以下事件过程,打开窗体运行后,单击窗体,消息框的输出结果为()。
Privatesubform_click()
dimkasinteger,nasinteger,masinteger
n=10:
m=1:
k=1
dowhilek<=n
m=m*2k=k+1
loop
MsgBoxmendsub
7.假定有以下程序段:
n=0
fori=1to3
forj=-4to-1
n=n+1
nextj
nexti运行完毕后,n的值是
闰年&九九乘法表
模块1
Sub闰年()
DimyearAsInteger
year=InputBox("请输入年份")
IfyearMod400=0Then
MsgBox"该年是闰年"
ElseIfyearMod100=0Then
MsgBox"该年不是闰年"
ElseIfyearMod4=0Then
MsgBox"该年是闰年"
Else
MsgBox"该年不是闰年"
EndIf
EndSub
Sub九九乘法表()
Worksheets("sheet1").Activate
DimnAsInteger,mAsInteger
n=1
m=1
DoWhilem<10
DoWhilen<10
Cells(m,n).Value=m*n
n=n+1
Loop
n=1
m=m+1
Loop
EndSub
Sub九九乘法表1()
Worksheets("sheet1").Activate
Fori=12To20
Forl=1To9
Cells(i,l).Value=(i-11)*l
Nextl
Nexti
EndSub
Sub九九乘法表2()
Worksheets("sheet2").Activate
Fori=1To9Step2
Forl=1To9
Cells(i,l).Value=i*l
Nextl
Nexti
EndSub
模块2
Sub闰年1()
DimxAsInteger
x=InputBox("请输入年份",输入年份)
IfxMod400=0Then
MsgBoxx&"为闰年"
ElseIfxMod100=0Then
MsgBoxx&"为平年"
ElseIfxMod4=0Then
MsgBoxx&"为闰年"
Else
MsgBoxx&"为平年"
EndIf
EndSub
Sub成绩()
DimxAsInteger
x=InputBox("请输入成绩",成绩等级判断)
Ifx>80Andx<=100Then
MsgBox"成绩为优秀",vbOKOnly,"成绩等级判断"
ElseIfx>=60Andx<=80Then
MsgBox"成绩为及格",vbOKOnly,"成绩等级判断"
Else
MsgBox"成绩不及格",vbOKOnly,"成绩等级判断"
EndIf
EndSub
循环结构-数组例题
模块1
Sub循环语句1()
DimiAsInteger
Fori=1To10
MsgBoxi,vbOKOnly,"循环语句"
Next
EndSub
Sub循环语句2()
DimiAsInteger
i=1
DoWhilei<=10
MsgBoxi,vbOKOnly,"循环语句"
i=i+1
Loop
EndSub
Sub循环语句until()
DimiAsInteger
i=1
DoUntili>10
MsgBoxi,vbOKOnly,"until语句"
i=i+1
Loop
EndSub
Sub循环语句while后测试()
DimiAsInteger
i=1
Do
MsgBoxi,vbOKOnly,"while后测试"
i=i+1
LoopWhilei<=10
EndSub
Sub循环语句until后测试()
DimiAsInteger
i=1
Do
MsgBoxi,vbOKOnly,"until后测试语句"
i=i+1
LoopUntili>10
EndSub
Sub循环次数测试()
DimnumAsInteger,counterAsInteger
num=100
counter=0
DoWhilenum<100
num=num+1
counter=counter+1
Loop
MsgBox"运行次数为"&counter
EndSub
Sub循环次数测试1()
DimnumAsInteger,counterAsInteger
num=100
counter=0
Do
num=num+1
counter=counter+1
LoopWhilenum<100
MsgBox"运行次数为"&counter
EndSub
Sub循环语句3()
DimiAsInteger
i=1
Whilei<=10
MsgBoxi,vbOKOnly,"循环语句"
i=i+1
Wend
EndSub
Sub循环语句4()
DimiAsInteger
Worksheets("sheet1").Activate
ForEachxInRange("A1:
C3")
x.Value=i
i=i+1
Next
EndSub
模块2
Sub数组赋值1()
Dimarr(3)AsString
DimiAsInteger
Fori=0To3
arr(i)=Range("A"&i+1)
MsgBoxarr(i)
Next
EndSub
Sub数组赋值2()
DimarrAsVariant
arr=Array(1,2,3,4)
Fori=0To3
MsgBoxarr(i)
Next
EndSub
Sub数组赋值3()
Dimarr
arr=[A1:
A4]
Fori=1To4
MsgBoxarr(i,i)
Next
EndSub
新建工作表
模块1
Subk1()
s=0
Fori=200To300
IfiMod3=0AndiMod5<>0Then
s=s+1
EndIf
Next
MsgBoxs
EndSub
Subk2()
a=0
b=1
Fori=1To25
'MsgBoxa&"和"&b'
Debug.Printa,b
a=a+b
b=a+b
Next
EndSub
Sub闰年()
n=InputBox("请输入年份:
")
If(nMod4=0AndnMod100<>0)Or(nMod400=0)Then
MsgBoxn&"是闰年"
Else
MsgBox"不是"
EndIf
EndSub
Sub图形()
Fori=1To4
Debug.PrintSpace(i);
Forj=1To2*(5-i)-1
Debug.Print"*";
Next
Debug.Print
Next
EndSub
5/7第六章1
模块1
Subtest1()
Worksheets("Sheet1").Range("A5:
D6").Value=22
MsgBox"工作表Sheet1内单元格A5中的值为"&Worksheets("Sheet1").Range("A5").Value
EndSub
Subtest4()
Worksheets
(1).Cells(5,1).Value=24
MsgBox"现在单元格A5的值为24"
EndSub
SubRandom()
DimmyRangeAsRange'设置对单元格区域的引用
SetmyRange=Worksheets("Sheet1").Range("A1:
D5")'对Range对象进行操作myRange.Formula="=RAND()"
myRange.Font.Bold=True
EndSub
Userform1
PrivateSubCommandButton1_Click()
TextBox1.PasswordChar=""
EndSub
PrivateSubCommandButton2_Click()
DimxAsInteger
x=TextBox2.Value*TextBox3.Value
Ifx=TextBox4.ValueThen
Label4.Caption="答对了"
Image1.Picture=LoadPicture("d:
\1.gif")
Else
Label4.Caption="答错了"
Image1.Picture=LoadPicture("d:
\a.jpg")
EndIf
EndSub
PrivateSubCommandButton3_Click()
UnloadMe
EndSub
PrivateSubCommandButton4_Click()
EndSub
PrivateSubLabel1_Click()
Label3.Caption=Time()
Label1.ForeColor=RGB(255,255,0)
Label1.Font.Name="微软雅黑"
EndSub
PrivateSubTextBox4_Change()
EndSub
PrivateSubUserForm_Click()
EndSub
Userform2
PubliciAsInteger
PrivateSubCommandButton1_Click()
IfTextBox2.Value=123456Then
UserForm1.Show
Else
i=i+1
Ifi<3Then
MsgBox"再输入一次密码!
"
Else
MsgBox"禁止进入系统!
"
UnloadMe
EndIf
EndIf
EndSub
PrivateSubCommandButton2_Click()
EndSub
PrivateSubUserForm_Click()
EndSub
5/146/14第六章2
Userform1
PubliciAsInteger
PrivateSubCommandButton1_Click()
TextBox1.PasswordChar=""
EndSub
PrivateSubCommandButton2_Click()
TextBox1.Value=""
EndSub
PrivateSubCommandButton3_Click()
IfTextBox1.Value=123456Then
UserForm2.Show
Else
i=i+1
Ifi<3Then
MsgBox("请重新输入密码!
")
Else
Unload
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据库
![提示](https://static.bdocx.com/images/bang_tan.gif)