You are on page 1of 66

VBA

2000 8


IT
Excel
N

HR

Excel ERP
ERP

Excel

Excel

Excel


Excel

ERP

200APM1

Excel Excel
VBA 80%
Excel

1
APM

2
Excel VBA

VBA

2012


Excel Excel
Excel Home
Excel Excel

E x c e l 3 0 0

Excel


1.

3

2.


3.

4. 
5. 
Excel VBA
VBA
Excel
VBA
VBA VBA

Excel
VBA

VBA
VBA
Excel Excel
VBA

VBA
Excel Home VBA

Excel
Excel VBA

Excel
Excel

VBA VBA
Excel VBA

VBA

VBA

Excel Home
2012 2 22

5

Excel VBA

Excel VBA
VBA

Excel Home http://www.excelhome.net

Excel
VBA Excel VBA
VBA

Windows Excel

VBA Office VBA


Office
Excel 2003 Windows XP Excel 2003
Excel 2007 Excel 2010 Excel
2007 Excel 2010


1 http://club.excelhome.net
2 http://t.excelhome.net ExcelHome
3http://weibo.com/iexcelhome( )http://t.qq.
com/excel_home

Excel

Excel Home Excel Home Excel


Home Excel Home

Excel Home

Excel Home
Excel Home

Excel Home

Excel Home(http://www.excelhome.net)
Excel Microsoft Office 1999 Kevin
Zhou Excel
Addins
Excel Home 2012 8
200 26 Office MVP
Office MVP
Office

Excel Home Office


2007
Excel Home Office
Office
Excel Home Office

Excel Home

Lets do it better

1 Excel VBA
1.1 Excel /2
1.1.1 /2
1.1.2 /2
1.1.3 /3
1.2 /5
1.2.1 /5
1.2.2 Excel /6
1.2.3 /8
1.3 /9
1.3.1 /9
1.3.2 /11
1.3.3 /13
1.4 /15
1.4.1 /15
1.4.2 /16
1.5VBAExcel /18
1.5.1 /18
1.5.2 /19
1.5.3VBA /21
1.5.4 VBA /21
1.5.5 VBA /21

2 VBA
2.1 /23
2.1.1 /23
2.1.2 /24

1
Excel VBA

2.2 /25
2.2.1 /25
2.2.2 /26
2.2.3 /26
2.2.4 /26
2.2.5 /26
2.2.6 /27
2.2.7 /27
2.3VBA VBE /27
2.3.1 VBE /27
2.3.2 /30
2.3.3 /30
2.3.4 /30
2.3.5 /31
2.3.6 /32
2.3.7 /32
2.3.8 /32
2.4 VBA /33
2.4.1 /34
2.4.2 /36
2.5 /37

3 Excel VBA

3.1 /40
3.1.1 /40
3.1.2VBA /40
3.1.3 VBA /41
3.2VBA /41
3.2.1 /41
3.2.2 Excel /42
3.2.3VBA /43

3.3 /45
3.3.1 /45
3.3.2 /45
3.3.3 /55
3.3.4 /55
3.4 /68
3.4.1 /68
3.4.2 /71
3.4.3 /72
3.5VBA /73
3.5.1 /73
3.5.2 /74
3.5.3 /76
3.5.4 /77
3.5.5 /78
3.6 /80
3.6.1VBA /80
3.6.2VBA /80
3.7VBA /82
3.7.1IfThen /82
3.7.2Select Case /86
3.7.3ForNext /89
3.7.4Do While /93
3.7.5Do Until /95
3.7.6For EachNext /96
3.7.7 /98
3.8Sub /99
3.8.1 VBA /100
3.8.2 Sub /100
3.8.3 /102
3.8.4 /103
3.9Function /105

3
Excel VBA

3.9.1 /106
3.9.2 /107
3.9.3 /108
3.9.4 /113
3.10 /113
3.10.1 /114
3.10.2 /114
3.10.3 /116

4.1 Excel /121


4.1.1VBA /121
4.1.2VBA Excel /123
4.1.3 /124
4.2 Application /125
4.2.1ScreenUpdating /125
4.2.2DisplayAlerts /128
4.2.3EnableEvents /130
4.2.4WorksheetFunction /133
4.2.5 Excel /134
4.2.6 /136
4.3 Workbook /138
4.3.1Workbook Workbooks /138
4.3.2 Workbook /141
4.3.3 /143
4.3.4ThisWorkbook ActiveWorkbook /146
4.4 Worksheet /147
4.4.1 Worksheet /147
4.4.2 /149
4.4.3Sheets Worksheets /157
4.5 Range /158

4.5.1 Range /159


4.5.2 /165
4.5.3 /176
4.5.4 /177
4.6 /182
4.6.1Names /182
4.6.2Comment /185
4.6.3 /186
4.7 /189
4.7.1 /189
4.7.2 /190
4.7.3 /191
4.7.4 /191
4.7.5 /192
4.7.6 /192
4.7.7 /193
4.7.8 /195
4.7.9 /195
4.7.10 /196
4.7.11 /198

5 Excel

5.1 Excel /200


5.1.1 Excel /200
5.1.2 VBA /201
5.1.3 /202
5.1.4 /203
5.2 Worksheet /204
5.2.1 Worksheet /204
5.2.2 Worksheet /204
5.2.3 Worksheet /209

5
Excel VBA

5.3 Workbook /210


5.3.1 Workbook /210
5.3.2 Workbook /210
5.3.3 Workbook /212
5.4 /213
5.4.1 MouseMove /213
5.4.2 /216
5.5 /219
5.5.1 /219
5.5.2 /222
5.5.3 /225

6.1 Excel /228


6.1.1 /228
6.1.2 /228
6.2 /231
6.2.1 /231
6.2.2 ActiveX /233
6.2.3 ActiveX /236
6.3 /236
6.3.1 InputBox /236
6.3.2 Application InputBox /238
6.3.3 MsgBox /241
6.3.4 Application FindFile /246
6.3.5 Application GetOpenFilename /247
6.3.6 Application GetSaveAsFilename /251
6.3.7 Application FileDialog /252
6.4 /253
6.4.1 /253
6.4.2 /254

6.4.3 /255
6.4.4 /256
6.4.5 /258
6.4.6 /261
6.4.7 /262
6.4.8 /264
6.5 Excel /265
6.5.1 /265
6.5.2 /266
6.5.3 /267
6.5.4 /269
6.5.5 /270
6.6 /270
6.6.1 /270
6.6.2 /280
6.6.3 /285

7.1 VBA /292


7.1.1 /292
7.1.2 /293
7.1.3 /294
7.2 VBA 3 /295
7.2.1 /295
7.2.2 /295
7.2.3 /295
7.3 Excel /295
7.3.1 /296
7.3.2 /298
7.3.3 Stop /300
7.3.4 /301

7
Excel VBA

7.3.5 /302
7.3.6 /303
7.4 /305
7.4.1 Go Error GoTo /305
7.4.2 On Error Resume Next /306
7.4.3 On Error GoTo 0 /307
7.5 /308
7.5.1 /309
7.5.2 /310
7.5.3 /312
7.5.4 /312
7.5.5 /312
7.5.6 /314

8
1
Excel VBA

VBA

VBA

VBA
VBA

Excel VBA

1.1 Excel
1.1.1





1.1.2

2
Excel VBA 1


Excel

1.1.3

11 12

11

12

3
Excel VBA


1000


4
Excel VBA 1

1.2

1.2.1
ExcelExcel

5
Excel VBA

1.2.2 Excel
13

1. A1

2.


4. 3.


13

14

6
Excel VBA 1

1. 1

2.

3. 3
2

4.

5. A3

6.

14 Excel

7
Excel VBA

1.2.3
15

1. A3
2.






3.



4.


15

8
Excel VBA 1




A1

1.3

1.3.1
16

16

17

9
Excel VBA

1.


<Alt+F8>

5.

2.

3.
<Ctrl+R>

4.

17


Excel <Ctrl+C>
Excel <Ctrl+C>

10
Excel VBA 1

1.3.2

11
Excel VBA




18

18


19

19

12
Excel VBA 1

110

110

111

111

1.3.3

112

13
Excel VBA

2
VBA


VBA

22
VBA 2

2.1
Excel VBA
VBA 1 1.2.2

2.1.1
21

1.
<Alt+F8>

2.

21

VBE

23
Excel VBA

2.1.2



Sub
End Sub


VBA

22

22

24
VBA 2

VBA
VBA

2.2

2.2.1
VBA VBA

25
Excel VBA

2.2.2
VBA VBA

Sub Function

2.2.3

2.2.4

2.2.5



Sheet1 A1 A1 Sheet1 A1
A1
(.) A1
A1.

Range("A1").Value

Value
A1

26
VBA 2

2.2.6

A1
A1 A1
(.) A1

Select AI

Range( A1 ).Select

2.2.7
VBA

2.3VBA VBE
2 2.1.1 VBA VBEVisual Basice
Editor VBA

2.3.1 VBE

VBE Excel
Excel VBE

<Alt+F11>

Visual Basic 23

23 VBE

27
Excel VBA

24

24 VBE

Visual BasicVisual Basic 25

25 Visual Basic VBE

28
VBA 2

26

26 VBE

ActiveX VBE
27

2.
1

3.

27 VBE

29
Excel VBA

2.3.2

VBE VBE

28

28VBE

2.3.3
VBE Excel 2003 VBE

2.3.4


29

30
VBA 2

29

2.3.5

Excel
Excel VBA Project( )
4 Excel Sheet ThisWorkbook
210

Excel
VBAProject(Book1.xls): Sheet
Workbook





VBA

210

Excel Excel

31
Excel VBA

2.3.6

2.3.7


211

211

VBA
VBA

2.3.8
212

32
VBA 2

3. 1.
Range(A1:B10).
Select

2.

212

7 7.3.4
VBE
213



<Ctrl+G>

213

2.4 VBA
Excel VBE

33
Excel VBA

2.4.1

VBA

214

214

215

1.

2.

215

34
VBA 2



2.4.1

216

2.

1.

216

217

1.

2.

217

35
Excel VBA

2.4.2
Step 1 218

2. 3. mysub

1.

4.

218

Public Sub mysub()

End Sub

Step 2 219

MsgBox VBA

36
VBA 2

219

Step 3 220

2.
/
( F5 )

1.

2-20

2.5

MsgBox

37
Excel VBA

VBA
MsgBox F1 221

1
MsgBox
F1

2
MsgBox

221 F1 MsgBox

Excel VBA
F1

38
Excel VBA 3

3
Excel VBA

VBA
VBA
VBA

39
Excel VBA

3.1
3.1.1

3.1.2VBA

VBA
VBA VBA

40
Excel VBA 3

3.1.3 VBA

VBA
,
VBA

3.2VBA
3.2.1

41
Excel VBA

3.2.2 Excel
Excel Excel

Excel
31

31Excel


Excel
Excel
Excel
32

32Excel

42
Excel VBA 3

3.2.3VBA

VBA ByteIntegerLong
SingleDoubleCurrencyDecimal
DateBoolean 31
String

31
VBA

Byte 1 0 255

Boolean 2 True False

Integer 2 - 2768 32767

Long 4 - 2147483648 2147483647

- 3.402823E38 - 1.401298E- 45
Single 4
1.401298E 45 3.402823E38

- 1.79769313486232E308 - 4.94065645841247E-
Double 8 324
4.94065645841247E324 1.79769313486232E308

Currency 8 - 922337203685477.5808 922337203685477.5807

+/- 79228162514264337593543950335
Decimal 14 +/- 7.9228162514264337593543950335
+/- 0.0000000000000000000000000001

100 1 1 9999 12 31
Date 8
0:00:00 23:59:59

String( ) 10 0 20

String( ) 1 65400

Object 4

String
Variant(
Double
)
String

Excel

33

43
Excel VBA

0 -32768 32767
String 255
Byte Integer


Long Date Single

33


32

32

005

200391

500 3000
2532.5

0 200
125.5

44
Excel VBA

VBA

VBA , VBA








120
4

4.1 Excel

4.1.1VBA

41

41

121
Excel VBA


VBA VBA
VBA ,

Excel

Excel
3.4
Excel

1. Microsoft
Visual Basic
Visual Basic

2. Microsoft Excel

4-2

122
4

4.1.2VBA Excel

VBA Excel

Excel

Worksheet

Workbook Range

Application Font

Excel


VBA Sheet1
A1 100

Application.Worksheets( Sheet1 ).Range( A1 ).Value = 100


VBA VBA

123
Excel VBA

4.1.3

VBA

124
4

VBA
41

41

Excel VBA


Application Excel

Workbook Excel Workbook

Worksheet Excel Worksheet

Range Excel

4.2 Application

Application Excel
Excel

4.2.1ScreenUpdating


38 2563
1548
3612


10

125
Excel VBA

Excel

Excel

A1:A10 100

Sub InputTest()
Cells.ClearContents
Range( A1:A10 ) = 100 A1:A10
MsgBox A1A10 100
Range( B1:B10 ) = 200
MsgBox B1B10 200
End Sub

B1:B10 20 0

43

126
4

43

ScreenUpdating True
FalseExcel

Sub InputTest_2()
Cells.ClearContents
Application.ScreenUpdating = False
Range( A1:A10 ) = 100 A1:A10
MsgBox A1A10 100
Range( B1:B10 ) = 200
MsgBox B1B10 200
Application.ScreenUpdating = True
End Sub


True

127
Excel VBA

43

44 2



ScreenUpdating ScreenUpdating False


4.2.2DisplayAlerts

128
4

Sub DelSht()
Dim sht As Worksheet
For Each sht In Worksheets
If sht.Name <> ActiveSheet.Name Then sht
sht.Delete sht
End If
Next
End Sub

45

45

Sub DelSht()
Dim sht As Worksheet
Application.DisplayAlerts = False
For Each sht In Worksheets
If sht.Name <> ActiveSheet.Name Then sht
sht.Delete sht
End If
Next
Application.DisplayAlerts = True
End Sub

129
Excel VBA

DisplayAlerts

Application DisplayAlerts
True False
False True

4.2.3EnableEvents

Application EnableEvents

Excel 5.1.2
Excel

130
Excel VBA

ExcelHome

http://product.dangdang.com/product.aspx?product_id=22876817

http://www.amazon.cn/mn/detailApp?asin=B009CSOMT6

http://book.360buy.com/11094550.html

http://product.china-pub.com/3684132

You might also like