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

前端之家收集整理的这篇文章主要介绍了VB热身:走进VBA&Excel高级应用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

几个vb小程序

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

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

  • 1到100累加
Private Sub Command1_Click()
Dim S%,I%
S = 0     ' 累加前变量S为0
For I = 1 To 100
  S = S + I
Next I
Print " S=",S

End Sub
  • 2步进循环
Private Sub Command1_Click()
Dim i As Integer,n As Integer
Dim sum As Integer
sum = 0
For i = 1 To 99 Step 2
sum = sum + i
Next i
Print sum

End 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 i

End Sub
  • do循环
Private Sub Command1_Click()
Dim i As Integer
Dim n As Integer
n = InputBox("请输入一个整数")   '11
For i = 2 To n - l    '从2到10做循环
If n Mod i = 0 Then
    Exit For                                      '提前结束循环
    End If
Next i
If i < n Then    '循环体结束后 i的值<11
            Print n & "不是素数"
Else                  '否则
          Print n & "是素数"
End If

End Sub

Private Sub Command2_Click()
Dim i As Integer
Dim sum As Integer
i = 1
sum = 0
Do
sum = sum + 1
i = i + 1
If i > 100 Then Exit Do
Loop
Print sum

End 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 = 1
F2 = 1
Print Fl; F2
For I = 3 To 15
F3 = F1 + F2
Print F3
F1 = F2
F2 = F3
Next
End 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 = y
End Sub
  • 判断打印
Private Sub Command1_Click()
Dim i As Integer,j As Integer,k As Integer
Dim n As Integer
n = 0
Print " 1的个数","2 的个数 ","5的个数 "
For i = 0 To 10
For j = 0 To 5
For k = 0 To 2
     If i + j * 2 + k * 5 = 10 Then
                n = n + l
               Print i,j,k
    End If
Next
Next
Next
Print 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 = 0
i = 0
n = l
t = l
Do While t > 0.00001
'Do While t > 0. 00001
e = e + t
i = i + l
n = n * i
t = l / n
Loop
Print e

End Sub
  • 循环的实现
Private Sub Command1_Click()
Dim i As Integer
Dim sum As Long
i = 1
sum = 0
Do While i <= 10000
  sum = sum + i
  i = i + 1
Loop
Print sum

End Sub

Private Sub Command2_Click()
Dim i As Integer,n As Integer
Dim sum As Integer
n = Val(Text1.Text)
sum = 0
i = 0
Do Until i > n
sum = sum + i
i = i + 1
Loop
Text2.Text = sum
End Sub
  • 最大公约数和最小公倍数
Private Sub Command1_Click()
Dim n%,m%,nm%,r%
m = Val(InputBox("m="))
n = Val(InputBox("n="))
nm = n * m
If m < n Then
   t = m
   m = n
   n = t
End If
    r = m Mod n
    Do While (r <> 0)
         m = n
         n = r
         r = m Mod n
    Loop
    Print "最大公约数=",n
    Print "最小公倍数=",nm / n
End 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程序编写能够更顺畅。

原文链接:https://www.f2er.com/vb/257008.html

猜你在找的VB相关文章