Escolar Documentos
Profissional Documentos
Cultura Documentos
Excel/VBA
VBA
VBA
Excel
1 / 20
Excel 2007Oce
Excel
VBE
2 / 20
Sub test()
Call MsgBox("Hello, world")
End Sub
Call MsgBox()()
Excel
Excel 2003
3 / 20
Excel 2007
test
4 / 20
VBA
Sub ()
End Sub
SubEnd Sub
(
)
Sub test()
Call MsgBox ("Hello, world")
End Sub
VBE
5 / 20
Sheet1A1(
)
Worksheets(Sheet1).Range(A1).Value = Hello
Worksheets(Sheet1).Range(A1).Value = 1.5
A1Hello1.5
Range(A1:A5)
Worksheets(Sheet1).Range(A1:A5).Value = Hello
A1A5Hello
Worksheets().
D224Cells
Worksheets(Sheet1).Cells(2,4)=1.5
Sheet1D24
Worksheets(Sheet1).Cells(2,4)=1+3
7+2
7-2
7*2
14
7/2
3.5
Mod
7Mod2
()
72
6 / 20
7^2
49
()
()
100/5^2-3=1
VBA
201
Sqr
Exp, Log
()
Rnd
01
Abs
WorksheetFunction.Pi
VBE
7 / 20
n
(Integer)
Sub test()
n
Dim n As Integer
n5
n=5
n
Call MsgBox(n)
End Sub
DimAs
Dim As
n=5n5n5
=
=
n=n+2n2(n2()n()
)
8 / 20
Integer
(-32768+32767)
Single
(4)
Double
(8)
String
Boolean
True()False()
Single(4)Double(8)
Single7Double16Double
()n(=100)
Const As =
5%
1.05
100
score001, score002,...,score100
(
)100
scores
9 / 20
Dim ( - 1) As
()(-1)10
(0), (1),...
100 x 8()a
(
)
()
Sub test()
Dim n As Integer
Dim a() As Double
A1
n = Worksheets("Sheet1").Cells(1, 1)
ReDim a(n - 1)
End Sub
10 / 20
ReDim
Dim () As
ReDim ( - 1)
1:
Sheet1A10
Sub test()
Dim x As Double
Dim msg As String
x = Worksheets(Sheet1).Cells(1, 1)
If x < 0 Then
msg = x & <0
Else
msg = x & >=0
End If
Call MsgBox (msg)
End Sub
Stringmsg&
If
If Then
()
Else
()
End If
11 / 20
Else ...
ElseIf
If 1 Then
1
ElseIf 2 Then
2
Else
1,2
End If
()
<, >
<=, >=
<>
x05
Endif
2:
For Next
A1A101,2,3,...,10
12 / 20
Sub test ()
Dim i As Integer
For i = 1 To 10
Worksheets(Sheet1).Cells(i, 1) = i
Next
End Sub
i1101ForNext
iFor Next
For = To (Step )
Next
Step
1010
For i = 10 To 0 Step -1
Exit For10x(9)
()i0
For i = 0 To 9
If x(i) < 0 Then
i0 = i
Exit For
EndIf
Next
()
Do While
Do While
Do While
Loop
13 / 20
Sub test ()
Dim x As Double
Dim i As Integer
x = 1 x
i=0
Do While x > 0
i=i+1
x = Worksheets(Sheet1).Cells(i, 1)
Loop
Call MsgBox(i)
End Sub
x = 1x(23)
xDo While
x>0x
Do While(
)
()
CFortran
A1
14 / 20
Sub test ()
Dim n As Integer
n = Worksheets("Sheet1").Range("A1").Value
Call testinteger(n)
End Sub
Sub testinteger(i As Integer)
Dim msg As String
If i Mod 2 = 1 Then
msg = i & " is odd"
Else
msg = i & " is even"
End If
Call MsgBox(msg)
End Sub
testtestinteger
Sub ()
Call (1, 2,...)
End Sub
Sub (1 As , 2 As ,...)
End Sub
15 / 20
2x4a
Sub test()
Const n As Integer = 2
Const m As Integer = 4
Dim a(n - 1, m - 1) As Integer
Dim i As Integer, j As Integer
a
Call set2darray(a)
a
For i = 0 To n - 1
For j = 0 To m - 1
Cells(i + 1, j + 1) = a(i, j)
Next
Next
End Sub
Sub set2darray(x() As Integer)
Dim i As Integer, j As Integer
For i = LBound(x, 1) To UBound(x, 1)
For j = LBound(x, 2) To UBound(x, 2)
x(i, j) = j + i * 4
Next
Next
End Sub
()
Sub ( () As )
16 / 20
()
LBound, UBound
LBound( [, ])
UBound( [, ])
LBound, UBound
**1
Sub test()
Dim i As Integer
i=0
()
Call Add1ByRef(i)
Call MsgBox("i = " & i) i=1
()
Call Add1ByVal(i)
Call MsgBox("i = " & i) i=1
End Sub
Sub Add1ByRef(i As Integer)
i=i+1
End Sub
Sub Add1ByVal(ByVal i As Integer)
i=i+1
End Sub
**
17 / 20
Add1ByRef, Add1ByVal1
(i)
ByValByRef
Sub (ByRef As )
Sub (ByVal As )
(Function)
Sin(x), Cos(x)VBA
Function
a, b Rectangle
Sub test2()
Dim a As Double, b As Double, s As Double
A1, B1
a = Cells(1, 1)
b = Cells(1, 2)
s = Rectangle(a, b)
Call MsgBox("s = " & s)
End Sub
Function Rectangle(a As Double, b As Double) As Double
Rectangle = a * b
End Function
18 / 20
Function (1 As , ...) As
=
End Function
**
()
num0num1, num2
num0
Dim num0 As Integer
Sub test1 ()
num1
Dim num1 As Integer
num0
num0 = 1
num1 = 1
End Sub
Sub test2 ()
Dim num2 As Integer
num0 = 2
num2 = 2
End Sub
num0test1, test2
num1test2(num2test1
)
19 / 20
(
)
DimPublic
Public As
DimPrivate
Dim As
Private As
()
20 / 20