VB热身:走进VBA&Excel高级应用

VB热身:走进VBA&Excel高级应用,第1张

概述几个vb小程序 在给大家讲Excel宏之前,让我们通过几个VB小例子来了解一下VB这门语言吧: 首先说明,VB编程是基于控件的,也就是你要先从左边控件窗口拖一个控件到窗口中去,然后双击控件进行编程。具体要拖那个控件来添加某一段程序,自己仔细琢磨琢磨就知道了。需要判断程序是哪一个控件的事件处理程序,这就不多说了。 1到100累加 Private Sub Command1_Click()Dim S% 几个vb小程序

在给大家讲Excel宏之前,让我们通过几个VB小例子来了解一下VB这门语言吧:

首先说明,VB编程是基于控件的,也就是你要先从左边控件窗口拖一个控件到窗口中去,然后双击控件进行编程。具体要拖那个控件来添加某一段程序,自己仔细琢磨琢磨就知道了。需要判断程序是哪一个控件的事件处理程序,这就不多说了。

1到100累加
Private Sub Command1_Click()Dim S%,I%S = 0     ' 累加前变量S为0For I = 1 To 100  S = S + INext IPrint " S=",SEnd Sub
2步进循环
Private Sub Command1_Click()Dim i As Integer,n As IntegerDim sum As Integersum = 0For i = 1 To 99 Step 2sum = sum + iNext iPrint sumEnd Sub
99乘法表的打印
Private Sub Command1_Click()For i = 1 To 9      For j = 1 To 9         se = i & "×" & j & "=" & i * j         Picture1.Print Tab((j - 1) * 9 + 1); se;      Next j      Picture1.Print   Next iEnd Sub
do循环
Private Sub Command1_Click()Dim i As IntegerDim n As Integern = inputBox("请输入一个整数")   '11For i = 2 To n - l    '从2到10做循环If n Mod i = 0 Then    Exit For                                      '提前结束循环    End IfNext iIf i < n Then    '循环体结束后 i的值<11            Print n & "不是素数"Else                  '否则          Print n & "是素数"End IfEnd SubPrivate Sub Command2_Click()Dim i As IntegerDim sum As Integeri = 1sum = 0Dosum = sum + 1i = i + 1If i > 100 Then Exit DoLoopPrint sumEnd Sub
计算圆的面积
Private Sub Command1_Click()    '定义点击方法  Dim r As Single,v As Single   '定义变量为单精度型  Const PI = 3.14 '定义常数PI  r = Val(inputBox("请输入圆半径","面积计算",0)) '输入消息事件窗口  v = PI * r ^ 2 '计算  Label1.Caption = Label1.Caption + Str$(v) '标签显示运算结果End Sub
简单计算示例
Private Sub Command1_Click()Dim Fl%,F2%,F3%,I%F1 = 1F2 = 1Print Fl; F2For I = 3 To 15F3 = F1 + F2Print F3F1 = F2F2 = F3NextEnd Sub
交换两个变量的值
Private Sub Command1_Click()  Dim temp As Single,x As Single,y As Single  x = Text1.Text  y = Text2.Text  temp = x  x = y  y = temp  Text3.Text = x  Text4.Text = yEnd Sub
判断打印
Private Sub Command1_Click()Dim i As Integer,j As Integer,k As IntegerDim n As Integern = 0Print " 1的个数","2 的个数 ","5的个数 "For i = 0 To 10For j = 0 To 5For k = 0 To 2     If i + j * 2 + k * 5 = 10 Then                n = n + l               Print i,j,k    End IfNextNextNextPrint n & "种方法"End Sub
摄氏度转换为华氏度
Private Sub Command1_Click()  Dim C As Single,F As Single  Dim str1 As String  C = Val(Text1.Text)  F = 9 / 5 * C + 32  str1 = "摄氏" + Str$(C) + "度相当于华氏" + Str$(F) + "度"  MsgBox str1,"转换温度"End Sub
循环变量
Private Sub Command1_Click()Dim i%,n&,t!,e!e = 0i = 0n = lt = lDo While t > 0.00001'do while t > 0. 00001e = e + ti = i + ln = n * it = l / nLoopPrint eEnd Sub
循环的实现
Private Sub Command1_Click()Dim i As IntegerDim sum As Longi = 1sum = 0Do While i <= 10000  sum = sum + i  i = i + 1LoopPrint sumEnd SubPrivate Sub Command2_Click()Dim i As Integer,n As IntegerDim sum As Integern = Val(Text1.Text)sum = 0i = 0Do Until i > nsum = sum + ii = i + 1LoopText2.Text = sumEnd Sub
最大公约数和最小公倍数
Private Sub Command1_Click()Dim n%,m%,nm%,r%m = Val(inputBox("m="))n = Val(inputBox("n="))nm = n * mIf m < n Then   t = m   m = n   n = tEnd If    r = m Mod n    Do While (r <> 0)         m = n         n = r         r = m Mod n    Loop    Print "最大公约数=",n    Print "最小公倍数=",nm / nEnd Sub
经验总结 Dim ab As integer,sum As single等价于
Dim ab%,sum! 数值转换为字符串方法有:
1、显示转换 使用str()函数
2、隐式转换 例如:Text1.text=100;
Text1.text=100 等价于
Text1.text=“100“ `’ For to ……Next
和C的风格还是有点不一样

作为学C出身的,写这么幼稚的VB程序,估计都会给学VB的同学笑话。本意只是想通过几个VB小程序,让没学过VB的同学对它有个了解,以便我接下来讲VBA程序编写能够更顺畅。

总结

以上是内存溢出为你收集整理的VB热身走进VBA&Excel高级应用全部内容,希望文章能够帮你解决VB热身:走进VBA&Excel高级应用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1266674.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存