You are on page 1of 65

www.zazhidown.

com

www.zazhidown.com


VBA

2000 8

IT
Excel
N

HR

ERP

Excel

ERP

Excel

www.zazhidown.com

Excel

Excel

Excel

ERP

200APM1

Excel Excel
VBA 80%
Excel

APM

www.zazhidown.com

Excel VBA

VBA
2012

Excel Excel
Excel Home
Excel Excel

E x c e l 3 0 0

Excel

1.

www.zazhidown.com

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

www.zazhidown.com

VBA VBA
Excel VBA

VBA
VBA

Excel Home
2012 2 22

www.zazhidown.com


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

www.zazhidown.com

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

www.zazhidown.com

2007
Excel Home Office
Office
Excel Home Office

Excel Home

Lets do it better

www.zazhidown.com

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

VBA
2.1 /23
2.1.1 /23
2.1.2 /24

www.zazhidown.com

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

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

www.zazhidown.com

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

www.zazhidown.com

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

www.zazhidown.com

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

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

www.zazhidown.com

Excel VBA
5.3 Workbook /210
5.3.1

Workbook /210

5.3.2

Workbook /210

5.3.3 Workbook /212

5.4

5.5

/213
5.4.1

MouseMove /213

5.4.2

/216

/219
5.5.1

/219

5.5.2 /222
5.5.3

/225

6.1

6.2

6.3

Excel /228
6.1.1

/228

6.1.2

/228

/231
6.2.1

/231

6.2.2

ActiveX /233

6.2.3

ActiveX /236

/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

www.zazhidown.com

6.5

6.6

6.4.3

/255

6.4.4

/256

6.4.5

/258

6.4.6

/261

6.4.7

/262

6.4.8

/264

Excel /265
6.5.1

/265

6.5.2

/266

6.5.3

/267

6.5.4

/269

6.5.5

/270

/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.3

7.2.1

/295

7.2.2

/295

7.2.3

/295

Excel /295
7.3.1

/296

7.3.2

/298

7.3.3

Stop /300

7.3.4

/301

www.zazhidown.com

Excel VBA

7.4

7.5

7.3.5

/302

7.3.6

/303

/305
7.4.1

Go Error GoTo /305

7.4.2

On Error Resume Next /306

7.4.3

On Error GoTo 0 /307

/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

www.zazhidown.com

1
Excel VBA

VBA

VBA

VBA
VBA

www.zazhidown.com

Excel VBA

1.1 Excel
1.1.1

1.1.2

www.zazhidown.com

Excel VBA

Excel

1.1.3

11 12

11

12

www.zazhidown.com

Excel VBA

1000

www.zazhidown.com

Excel VBA

1.2

1.2.1
ExcelExcel

www.zazhidown.com

Excel VBA

1.2.2 Excel
13

1. A1

2.

3.

4.

13

14

www.zazhidown.com

Excel VBA

1. 1

2.

3. 3
2

4.

5. A3

6.

14 Excel

www.zazhidown.com

Excel VBA

1.2.3
15

1. A3

2.

3.

4.

15

www.zazhidown.com

Excel VBA

A1

1.3

1.3.1
16

16

17

www.zazhidown.com

Excel VBA

1.


<Alt+F8>

5.

2.

3.
<Ctrl+R>

4.
17

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

10

www.zazhidown.com

Excel VBA

1.3.2

11

www.zazhidown.com

Excel VBA

18

18

19

19

12

www.zazhidown.com

Excel VBA

110

110

111

111

1.3.3
112

13

www.zazhidown.com

Excel VBA

2
VBA

VBA

22

www.zazhidown.com

VBA

2.1
Excel VBA
VBA 1 1.2.2

2.1.1
21
1.
<Alt+F8>

2.

21

VBE

23

www.zazhidown.com

Excel VBA

2.1.2

Sub
End Sub

VBA

22

22

24

www.zazhidown.com

VBA

VBA
VBA

2.2

2.2.1
VBA VBA

25

www.zazhidown.com

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
A1

Value

26

www.zazhidown.com

VBA

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

www.zazhidown.com

Excel VBA

24

24 VBE

Visual BasicVisual Basic 25

25 Visual Basic VBE

28

www.zazhidown.com

VBA

26

26 VBE

ActiveX VBE
27

2.

3.

27 VBE

29

www.zazhidown.com

Excel VBA

2.3.2
VBE VBE

28

28VBE

2.3.3
VBE Excel 2003 VBE

2.3.4

29

30

www.zazhidown.com

VBA

29

2.3.5
Excel
Excel VBA Project( )
4 Excel Sheet ThisWorkbook
210

VBAProject(Book1.xls):

Excel
Sheet
Workbook

VBA

210

Excel Excel

31

www.zazhidown.com

Excel VBA

2.3.6

2.3.7

211

211

VBA
VBA

2.3.8
212

32

www.zazhidown.com

VBA

1.
Range(A1:B10).
Select

3.

2.

212

7 7.3.4
VBE
213

<Ctrl+G>

213

2.4 VBA
Excel VBE

33

www.zazhidown.com

Excel VBA

2.4.1
VBA


214

214

215

1.

2.

215

34

www.zazhidown.com

VBA

2.4.1

216

2.

1.

216

217

1.

2.

217

35

www.zazhidown.com

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

www.zazhidown.com

VBA

219

Step 3 220
2.
/
( F5 )

1.

2-20

2.5
MsgBox

37

www.zazhidown.com

Excel VBA

VBA
MsgBox F1 221

1
MsgBox
F1

2
MsgBox

221 F1 MsgBox

Excel VBA
F1

38

www.zazhidown.com

Excel VBA

3
Excel VBA

VBA
VBA
VBA

39

www.zazhidown.com

Excel VBA

3.1
3.1.1

3.1.2VBA

VBA
VBA VBA

40

www.zazhidown.com

Excel VBA

3.1.3 VBA

VBA
,
VBA

3.2VBA
3.2.1
A

41

www.zazhidown.com

Excel VBA

3.2.2 Excel
Excel Excel

Excel
31

31Excel

Excel
Excel
Excel
32

32Excel

42

www.zazhidown.com

Excel VBA

3.2.3VBA

VBA ByteIntegerLong
SingleDoubleCurrencyDecimal
StringDateBoolean 31
31

VBA

Byte

0 255

Boolean

True False

Integer

- 2768 32767

Long

- 2147483648 2147483647

Single

- 3.402823E38 - 1.401298E- 45
1.401298E 45 3.402823E38

Double

- 1.79769313486232E308 - 4.94065645841247E324
4.94065645841247E324 1.79769313486232E308

Currency

- 922337203685477.5808 922337203685477.5807

Decimal

14

+/- 79228162514264337593543950335
+/- 7.9228162514264337593543950335
+/- 0.0000000000000000000000000001

Date

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

String( )

10

0 20

String( )

1 65400

Object

Variant(
)

String
Double
String

Excel

33

43

www.zazhidown.com

Excel VBA

String

-32768 32767

Integer

0
255
Byte

Long

Single

Date

33

32

32

005

200391

500 3000

2532.5

0 200

125.5

44

www.zazhidown.com

Excel VBA

VBA

VBA ,

VBA

120

www.zazhidown.com

4.1 Excel
4.1.1VBA

41

41

121

www.zazhidown.com

Excel VBA

VBA VBA
VBA ,

Excel


Excel
3.4
Excel

1. Microsoft
Visual Basic
Visual Basic

2. Microsoft Excel

4-2

122

www.zazhidown.com

4.1.2VBA Excel
VBA Excel
Excel

Worksheet
Range

Workbook

Application
Excel

Font

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

VBA VBA

123

www.zazhidown.com

Excel VBA

4.1.3
VBA

124

www.zazhidown.com

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

www.zazhidown.com

Excel VBA

Excel

Excel

A1:A10 100
Sub InputTest()
Cells.ClearContents



MsgBox A1A10 100

Range( A1:A10 ) = 100


Range( B1:B10 ) = 200


A1:A10

MsgBox B1B10 200


End Sub

B1:B10 20 0

43

126

www.zazhidown.com

43


ScreenUpdating True
FalseExcel

Sub InputTest_2()
Cells.ClearContents

Application.ScreenUpdating = False

Range( A1:A10 ) = 100


MsgBox A1A10 100
Range( B1:B10 ) = 200
MsgBox B1B10 200

Application.ScreenUpdating = True

End Sub



A1:A10

True

127

www.zazhidown.com

Excel VBA

43

44 2

ScreenUpdating

ScreenUpdating False

4.2.2DisplayAlerts

128

www.zazhidown.com

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

sht.Delete

End If



sht
sht

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.Delete
End If
Next

Application.DisplayAlerts = True

End Sub


sht
sht

129

www.zazhidown.com

Excel VBA

DisplayAlerts
Application DisplayAlerts
True False
False True

4.2.3EnableEvents
Application EnableEvents

Excel 5.1.2
Excel

130

www.zazhidown.com

You might also like