Вы находитесь на странице: 1из 87

(Excel VBA)

costask@otenet.gr
.: 6944261906
.. 2010

4 5

VBA
&
(Debugging & Error Handling)

Office

&
. - costask@otenet.gr

4
VBA
&
(Debugging & Error Handling)

. - costask@otenet.gr

&
() Excel
Excel
(Workbook)
(Workbook) Excel

(WorkSheets)
Excel
(Rows)
(Columns)

(Cell)
,
, (Range)
. - costask@otenet.gr

&
Excel VBA,
WorkBook WorkSheet,
:
Private Sub InsertSheetButton_Click()
ThisWorkbook.Sheets.Add
MsgBox : " & ThisWorkbook.Sheets.Count & " "
End Sub

Private Sub DeleteSheetButton_Click()


ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count - 1).Delete
MsgBox : " & ThisWorkbook.Sheets.Count & " "
End Sub

Private Sub RenameSheetButton_Click()


NewName = InputBox( : ", ")
ThisWorkbook.Sheets(1).Name = NewName
End Sub
. - costask@otenet.gr

&
,
(dropdown list).
.
Private Sub OpenWorkbooksCombo_Change()
Workbooks(OpenWorkbooksCombo.Value).Activate
End Sub

Private Sub ExitButton_Click()


Unload Me
End Sub
. - costask@otenet.gr

Private Sub UpdateButton_Click()


Dim wb As Workbook
'
OpenWorkbooksCombo.Clear
' Workbook
For Each wb In Workbooks
'
OpenWorkbooksCombo.AddItem wb.Name
Next wb
End Sub
6

(Charts) VBA (1/4)


(Chart) Excel
VBA,
.
, ,

. ,
( ),

(ActiveSheet).

. - costask@otenet.gr

(Charts) VBA (2/4)



(Range).

. - costask@otenet.gr

(Charts) VBA (3/4)

'
Private Sub ExitButton_Click()
Unload Me
End Sub

'
' .
Private Sub UserForm_Activate()
CreateChartForm.Show vbModeless
' Modal
ChartTypeComboBox.Style = fmStyleDropDownList ' ListBox
ChartTypeComboBox.AddItem "3D-Columns" ' ListBox
ChartTypeComboBox.AddItem "3D-Bars"
ChartTypeComboBox.AddItem "3D-Pie"
ChartTypeComboBox.Value = ChartTypeComboBox.List(0) '.
End Sub
. - costask@otenet.gr

(Charts) VBA (4/4)

. - costask@otenet.gr

10

(Debugging)
Bug (moth) Mark II
Harvard, 9 1947.
(debugging), .

Harvard 9 . 1947
. - costask@otenet.gr

11

(Debugging)

.

( ).
Bill Gates ( Microsoft) :
Microsoft Testers (),
Developers ()
.
,

, .

. - costask@otenet.gr

12

(Debugging) VBA
VBA,

.
:
-
BreakPoints ( )


,
(Watches, Locals, Immediate)

. - costask@otenet.gr

13

(Debugging) VBA
Break Points
Break Point
, VBA

Break point.
point
Break Mode,
,
Immediate.
Immediate
Break Point
Point
, (Dim).
Break Point
oint
F5,
"Continue"
Continue Run,
Run
-.
Break Points
Points
WorkBook
.
. - costask@otenet.gr

14

(Debugging) VBA


VBA,
BreakPoint .

VBA
Menu :
Toggle BreakPoint.
F9
Menu
Debug

VBA

BreakPoint,


.
. - costask@otenet.gr

15

(Debugging) VBA
-
(Step Into / Over / Out )
-
F8
(Sub) cursor
Break-Point.
Point
-,

.
-
,
-
SHIFT+F8 "Step Over"
Over
-
.
CTRL+SHIFT+F8 , "Step
Out"
Out -

.

Run To Cursor
VBA
cursor.
Break Point.
. - costask@otenet.gr

16

(Debugging) VBA
Debug


VBA

Immediate, Locals
Watches

,
.

. - costask@otenet.gr

17

(Debugging) VBA
Watch
Watch
Watch
(Watches)
Watches
Break mode
mode Watch
Watch True
Watch
Watch .
Watch
Watch View.
View

Watch,
Watch Add
Watch
Watch Debug,
Debug
Add Watch
Watch
.
, Watch Type
Type
Watches.
Watches
Watches
Project.

. - costask@otenet.gr

18

(Debugging) VBA
Locals
Locals
Locals ()
Global -.

Locals.
Locals
Locals
Locals View.
View

. - costask@otenet.gr

19

(Debugging) VBA
Immediate
Immediate
Immediate
Break mode
mode
. View
View
CTRL+G.
:
?ActiveCell.Address
$A$10

' ( ?)
' ( )

? ,
ENTER .
:
Range("A1").Value = "" <ENTER>
, :
.
:
For j=1 To 10 : Debug.Print MyArray(j) : Next j <ENTER>

. - costask@otenet.gr

20

(Debugging) VBA
Debug.Print
Debug.Print
Debug.Print
/
Immediate.
Immediate :
'
' VBA
'
Debug.Print
MyArray(4) ' MyArray(4)
Debug.Assert
Debug.Assert ( )
,
True
False.
False
False, VBA Break
Mode
Mode . :
Dim i As Integer
i = 200
Debug.Assert (i < 90) ' False,
' Break Mode
. - costask@otenet.gr

21

(Debugging) VBA
(Error Codes) VBA (1/3)
VBA Error Codes
Error
code

Error message

3
5
6
7
9
10

Return without Gosub


Invalid procedure call
Overflow
Out of memory
Subscript out of range
Duplicate definition (versions 5.0 and 7.0)

10

This array is fixed or temporarily locked (version97)

11
13
14
16
16
17
18
23
28
35
35
47
48
49
51
52
53
54
55

Division by zero
Type mismatch
Out of string space
String formula too complex (versions 5.0 and 7.0)
Expression too complex (version 97)
Can't perform requested operation
User interrupt occurred
Resume without error
Out of stack space
Sub or function not defined (versions 5.0 and 7.0)
Sub, function, or property not defined (version 97)
Too many DLL application clients (version 97)
Error in loading DLL
Bad DLL calling convention
Internal error
Bad file name or number
File not found
Bad file mode
File already open

. - costask@otenet.gr

VBA Error Codes


Error
code
57
58
59
61
61
63
67
68
70
71
74
75
76
91
91
92
93
94
95
298
320
321
322
323
325
327

Error message
Device I/O error
File already exists
Bad record length
Disk full
Input past end of line
Bad record number
Too many files
Device unavailable
Permission denied
Disk not ready
Can't rename with different drive
Path/File access error
Path not found
Object variable not set (versions 5.0 and 7.0)
Object variable or with block variable not set (version 97)
For Loop not initialized
Invalid pattern string
Invalid use of Null
User-defined error (versions 5.0 and 7.0 only)
System DLL could not be loaded (version 97)
Can't use character device names in specified file names
(version 97)
Invalid file format (version 97)
Can't create necessary temporary file (version 97)
Can't load module; invalid format (versions 5.0 and 7.0)
Invalid format in resource file (version 97)
Data value named was not found (version 97)

22

(Debugging) VBA
(Error Codes) VBA
VBA Error Codes

VBA Error Codes


Error
code
328
335
336
337
338
360
361
363
364
365
368
371
380
381
382
383
385
387
393
394
400
402
419

Error message
Illegal parameter; can't write arrays (version 97)
Could not access system registry (version 97)
ActiveX component not correctly registered (version 97)
ActiveX component not found (version 97)
ActiveX component did not correctly run (version 97)
Object already loaded (version 97)
Can't load or unload this object (version 97)
Specified ActiveX control not found (version 97)
Object was unloaded (version 97)
Unable to unload within this context (version 97)
The specified file is out of date. This program requires a
newer version (version 97)
The specified object can't be used as an owner form for
Show (version 97)
Invalid property value (version 97)
Invalid property -array index (version 97)
Property Set can 't be executed at run time (version 97)
Property Set can't be used with a read-only property
(version 97)
Need property-array index (version 97)
Property Set not permitted (version 97)
Property Get can't be executed at run time (version 97)
Property Get can't be executed on write-only property
(version 97)
Form already displayed; can't show modally (version 97)
Code must close topmost modal form first (version 97)
Permission to use object denied (version 97)

. - costask@otenet.gr

(2/3)

Error
code
422
423
424
425
429
430
430
432
438
440
440
442
443
445
446
447
448
449
449
450
450
451

Error message
Property not found (version 97)
Property or method not found
Object required
Invalid object use (version 97)
ActiveX component can't create object or return reference to
this object (version 97)
Class doesn't support OLE Automation
Class doesn't support Automation (version 97)
File name or class name not found during Automation
operation (version 97)
Object doesn't support this property or method
OLE Automation error
Automation error (version 97)
Connection to type library or object library for remote
process has been lost (version 97)
Automation object doesn't have a default value (version 97)
Object doesn't support this action
Object doesn't support named arguments
Object doesn't support current locale settings
Named argument not found
Argument not optional
Argument not optional or invalid property assignment
(version 97)
Wrong number of arguments
Wrong number of arguments or invalid property assignment
(version 97)
Object not a collection

23

(Debugging) VBA
(Error Codes) VBA
VBA Error Codes
Error
code
452
453
454
455
457
458
459
460
461
480
481
482
483
484
485
486
735
744
746

Error message
Invalid ordinal
Specified DLL function not found
Code resource not found
Code resource lock error
This key is already associated with an element of this
collection (version 97)
Variable uses a type not supported in Visual Basic (version
97)
This component doesn't support events (version 97)
Invalid clipboard format (version 97)
Specified format doesn't match format of data (version 97)
Can't create AutoRedraw image (version 97)
Invalid picture (version 97)
Printer error (version 97)
Printer driver does not support specified property (version
97)
Problem getting printer information from the system. Make
sure the printer is set up correctly (version 97)
Invalid picture type (version 97)
Can't print form image to this type of printer (version 97)
Can't save file to Temp directory (version 97)
Search text not found (version 97)
Replacements too long (version 97)

. - costask@otenet.gr

(3/3)

VBA Error Codes


Error
code
1000
1001
1002
1003
1004
1005
1006
31001
31004
31018
31027
31032
31036
31037

Error message
Classname does not have propertyname property (versions
5.0 and 7.0)
Classname does not have methodname method (versions 5.0
and 7.0)
Missing required argument argumentname (versions 5.0 and
7.0)
Invalid number of arguments (versions 5.0 and 7.0)
Methodname method of classname class failed (versions 5.0
and 7.0)
Unable to set the propertyname property of the classname
class (versions 5.0 and 7.0)
Unable to get the propertyname property of the classname
class (versions 5.0 and 7.0)
Out of memory (version 97)
No object (version 97)
Class is not set (version 97)
Unable to activate object (version 97)
Unable to create embedded object (version 97)
Error saving to file (version 97)
Error loading from file (version 97)

24

(Debugging) VBA
Watches

(1/4)


300
.

Watches

. - costask@otenet.gr

25

(Debugging) VBA
Watches

(2/4)


BreakPoint

2 Watches
.

. - costask@otenet.gr

26

(Debugging) VBA
Watches

(3/4)

Watch

Val().


(Double).

. - costask@otenet.gr

27

(Debugging) VBA
Watches

(4/4)


,

,

.

. - costask@otenet.gr

28

(Debugging) VBA
Locals & Watches

(1/3)

BreakPoint

F8
(Step Into)
Into


Watch

SumTextBox.Value

Locals ( View),
Sum2 ( ).

. - costask@otenet.gr

29

(Debugging) VBA
Locals & Watches

(2/3)

(SumTextBox.Value = Sum2)
Sum2 False
.

. - costask@otenet.gr

30

(Debugging) VBA
Locals & Watches

(3/3)

, Val()
( String )
.
. - costask@otenet.gr

31

(Debugging) VBA
Immediate

(1/2)

Immediate


Sum1

( 333).

.

. - costask@otenet.gr

32

(Debugging) VBA
Immediate

(2/2)


Sum1
: Sum1 = Sum2
False.
False

. - costask@otenet.gr

33

VBA

On Error
On Error
Error VBA
.
(Error trapping).

Resume
Resume
Resume VBA
, -
On Error.
Error

Err
Err
Err (Error) ..
Err.Number Err.Description
. Clear Raise
.

Error()
Error()
Error()
, .

. - costask@otenet.gr

34

VBA
On Error

(1/3)

(Error Handling) VBA


On Error. VBA
(Run time error).
On Error:
1) On Error Goto 0
2) On Error Resume Next
3) On Error Goto <label>:
, On Error Goto 0, (Default)
Default VBA.
, VBA ,
, standard Message Box,
(Debug mode)
mode
.
On Error Goto 0
(Error Handler).

. - costask@otenet.gr

35

VBA
On Error

(2/3)

On Error Resume Next, VBA


.
On Error Resume Next,
.
(errors) .
, Err.Number.
0, (
) .
:

On Error Resume Next


N = 1 / 0
'
If Err.Number <> 0 Then '
N = 1
'
End If
#11 (Division By Zero).
On Error Resume Next , ,
If Err.Number .End If, .
. - costask@otenet.gr

36

VBA
On Error

(3/3)

On Error Goto <label>: VBA



<label>.
label>
:

On Error Goto ErrHandler:


N = 1 / 0
'
'
'
'
Exit Sub
ErrHandler:
'
Resume Next
End Sub
. - costask@otenet.gr

37

VBA
Resume

(1/3)

Resume , VBA
.
(
Error Handling Code),
(endless loop)
(Error Handling Block).
lock

:
On Error GoTo ErrHandler:
Worksheets("NewSheet").Activate '
Exit Sub
ErrHandler:
If Err.Number = 9 Then
'
Worksheets.Add.Name = "NewSheet" '
Resume
'
End If
. - costask@otenet.gr

38

VBA
Resume

(2/3)

Resume Resume Next.


VBA
.
( #11 - Division By Zero).
(Error Handling Block)
lock
(error)
:
On Error GoTo ErrHandler:
N = 1 / 0
'
Debug.Print N
Exit Sub
ErrHandler:
N = 1
Resume Next

. - costask@otenet.gr

'
'
'
39

VBA
Resume

(3/3)

Resume Resume <label>: .


VBA <label>: .
.
:
On Error GoTo ErrHandler:
N = 1 / 0
'
'
'
'
Label1:
'
'
'
Exit Sub
ErrHandler:
Resume Label1:

' Label1:

Resume (Error
Handling Block), (Clear ) Err.
. - costask@otenet.gr

40

VBA
Err
Err
Err (Error)
Error

.

Number
Description
Source

(Properties):
(Error Code)
()

(Methods):
Clear
() Err
(user-defined)
Raise
Err:
'

MsgBox : & Err.Description


Err.Clear

' Err

Err.Raise 2000

' 2000

. - costask@otenet.gr

41

VBA
H Error()
Error()
.
: Error( errornumber )
errornumber o . errornumber
.
, #13, :
Msg = " : " & Error(Err.Number) & vbCrLf & _
" : " & Error(Err.Number)
:

. - costask@otenet.gr

42

VBA
OnError, Resume, Err, Error(), Debug.Print (1/5)

. - costask@otenet.gr

43

VBA
OnError, Resume ,Err, Error(), Debug.Print (2/5)

. - costask@otenet.gr

44

VBA
OnError, Resume, Err, Error(), Debug.Print (3/5)

. - costask@otenet.gr

45

VBA
OnError, Resume ,Err, Error(), Debug.Print (4/5)

. - costask@otenet.gr

46

VBA
OnError, Resume ,Err, Error(), Debug.Print (5/5)

. - costask@otenet.gr

47

()

(Data Base)
,
.
..
(Relational) .

RDBMS (Relational DB Management System).
RDBMS Oracle, SQL Server, MySQL, MS-Access, ..
, de facto standard
SQL (Structured Query Language).

,

RDBMS.
(Programming Interfaces) SQL
RDBMS.
. - costask@otenet.gr

48


SQL

(1/4)


SQL (Structured Query
Language), :
DDL (Data Definition Language, ),
.
DML (Data Manipulation Language, ),
.
DCL (Data Control Language, ),

.
SQL
SQL,
SQL
.
SQL (char(n), varchar(n),
Int,, smallint, Numeric(p, d), real, double precision, float(n), date, time)
, ..
Create domain Surname char(20); Surname 20 .
. - costask@otenet.gr

49


SQL

(2/4)


Members
Create Table Members( MemberCode integer not null,
Surname
char(20),
Name
char(20),
City
char(15),
BirthDate
date,
ClubCode
integer
primary key MemberCode )
AFM, 9 , Members
Alter table Members add (AFM char(9));
AFM Members
Alter table Members drop AFM;
Members
Drop table Members;

. - costask@otenet.gr

50


SQL

(3/4)

/ /
Members
Insert into Members (MemberCode, Surname, Name, City, BirthDate, ClubCode )
values (126, , , , null, 140);
ClubCode Members
Update Members Set ClubCode = 150 Where ClubCode = 140;

Members
Delete from Members Where City = ;

. - costask@otenet.gr

51


SQL

(4/4)


DML SQL Select:
:

Select 1, 2, n *
From 1, 2, , m
Where ;
(*) .

Name, Surname, City Members


Select Name, Surname, City from Members;

Members
Select * from Members;
Members
Select * from Members where City = ;
Members
Select * from Members where City = OR City = ;
. - costask@otenet.gr

52



,
, Microsoft
, MDAC
(Microsoft Data Access Components). :
ADO (ActiveX Data Objects),
OLE DB
ODBC (Open Database Connectivity)
ADO ,
OLE DB. .
To OLE DB COM
(.., File System, ).
ODBC , standard,
.
. - costask@otenet.gr

53

MDAC .
. - costask@otenet.gr

54

- ODBC
ODBC (Open Dataase Connectivity) standard
(DataBases).
DataBases Microsoft
1992, 3.8 ( 2009).

ODBC Manager

ODBC Driver 1

ODBC Driver 2

DataBase 1

DataBase 2

...

ODBC Driver N

DataBase N

ODBC
..
ODBC Manager.
Manager
ODBC Driver
Driver ..
. , .. .
. - costask@otenet.gr

55

ODBC DSN
Access

(1/3)

ODBC
DSN
(Data Source Name)
.

6

.

(1)

(2)

(3)

. - costask@otenet.gr

56

ODBC DSN
Access

(2/3)

(5)

(4)


Access


DSN
(6)

. - costask@otenet.gr

57

ODBC DSN
Access

(3/3)


Access

ActiveSheet Excel

. - costask@otenet.gr

58

- ADO
ADO (ActiveX Data Objects )

(Programming Interface)
Interface
, ..
Microsoft 1996 Microsoft ActiveX
component, IIS
IIS Microsoft
VBA Excel

(reference)
reference ADO
ADO (ADO
Object Library). :
Tools VBA
References Microsoft ActiveX Data Objects 2.8
Library
Library ( )
ADO COM component ODBC.
ODBC
ODBC .
, .
ADO RDO (Remote Data Objects)
DAO (Data Access Objects).
. - costask@otenet.gr

59

- ADO
ADO
*:
1. (Connection object)
(: Set MyConn = New ADODB.Connection )

2. Recordset (Recordset object)


(: Set MyRecSet = New ADODB.Recordset )

3.
(: MyConn .Open ConnectionString, UserID, Password, Options )

4. / Recordset
(: MyRecSet.Open Source, Connection, CursorType, LockType, Options )

5. Recordset Excel
(: RangeExpression.CopyFromRecordset MyRecSet)

6. Recordset

(: MyRecSet.Close)

7.

(: MyConn.Close)

8. (free)
free Recordset (: Set MyRecSet = Nothing)
9. (free) (: Set MyConn = Nothing)
* ADO,
. - costask@otenet.gr

60

ADO
Access

(1/2)

(1)


MS-Access
Access


Temp
Temp
(ActiveSheet)
Excel.
. - costask@otenet.gr

(2)


Excel ..
MS Access

61

ADO
Access

. - costask@otenet.gr

(2/2)

62

- ADO
ADO
:
1. (Connection object)
(: Set MyConn = New ADODB.Connection )

2. Recordset (Recordset object)


(: Set MyRecSet = New ADODB.Recordset )

3.
(: MyConn .Open ConnectionString, UserID, Password, Options )

4. Recordset ..
(: MyRecSet.Open TableName, Connection, CursorType, LockType )

5. Recordset

(: MyRecSet .AddNew)

6. Recordset
(: MyRecSet .Fields() = Range(A5).Value

7. .. Recordset

(: MyRecSet .Update)

8. Recordset

(: MyRecSet.Close)

9.

(: MyConn.Close)

10. (free)
free Recordset (: Set MyRecSet = Nothing)
11. (free) (: Set MyConn = Nothing)
. - costask@otenet.gr

63

ADO
Access

(1/2)

(1)

(2)


MS-Access
Access
ActiveSheet
Excel


Temp.
Temp

(3)

(4)

. - costask@otenet.gr

64

ADO
Access (2/2)

. - costask@otenet.gr

65

5
Office
(
)
&

. - costask@otenet.gr

66


MS-Word, MS-PowerPoint, MS-Outlook
VBA
Excel MS-Office (.. MS-Word, MSPowerPoint, MS-Outlook ).
VBA VBA,
Word PowerPoint
.
MS-Word Document:

MS-PowerPoint :

(Slide) :

. - costask@otenet.gr

67


MS-Word MS-PowerPoint () - (1/6)


1
5 Word
Document .
. - costask@otenet.gr

68


MS-Word MS-PowerPoint () - (2/6)

. - costask@otenet.gr

69


MS-Word MS-PowerPoint () - (3/6)



1 5

PowerPoint
.
. - costask@otenet.gr

70


MS-Word MS-PowerPoint () - (4/6)

. - costask@otenet.gr

71


MS-Word MS-PowerPoint () - (5/6)

. - costask@otenet.gr

72


MS-Word MS-PowerPoint - () - (6/6)

. - costask@otenet.gr

73


MS-Outlook - () - (1/2)


e-mails

,


.
. - costask@otenet.gr

74


MS-Outlook - () - (2/2)

. - costask@otenet.gr

75


e-mail (SendMail)

(1/2)

(1)
(2)

(3)

. - costask@otenet.gr

76


e-mail (SendMail)

. - costask@otenet.gr

(2/2)

77


VBA
Excel
.



VBA, :

Excel
Excel
VBA

Excel
(
Windows File System)
. - costask@otenet.gr

78

(1/7)

,
(ActiveSheet),
(Excel)
.
. CheckBox
,
( ).
Windows,
.
,
( ).

. - costask@otenet.gr

79

(2/7)



Windows

. - costask@otenet.gr

80

(3/7)


3


.

. - costask@otenet.gr

81

(4/7)

*.xls ActiveSheet
. - costask@otenet.gr

82

(5/7)

*.xls ActiveSheet
. - costask@otenet.gr

83

(6/7)

. - costask@otenet.gr

84

(7/7)



. False

. - costask@otenet.gr

85

. - costask@otenet.gr

86



e-mail: costask@otenet.gr
.: 6944 261906
. - costask@otenet.gr

87

Вам также может понравиться