You are on page 1of 194

:

...


...

"

"

77 ........................................................................................................
71...................................................................................

: 71 ......................................................................
0202 71 ................................................................. :0202
78..................................................................
0202 78..................................................................... 0202
71 ...............................................................................
: 17 ...................................................................
(17 ............................................................................ )The excel visual basic editor
(11...................................................................... )The project window
(11..................................................................... )The Code Windows
(11 ............................................................. )Properties Window
(11 ............................................................. )The Immediate Window
(12 .................................................................... )The Locals Window
(12 ....................................................................... )The Watch Window
18 ...................................................................................
: 11.......................................................................
(17........................................... )Use Relative References
17..........................................................................
11 ...................................................................................
11 ................................................................................................
5

11 .......................................................................

: 18........................................................

18......................................................................
(17.................................................................................... )Comments
11 ...................................................................
21 ......................................................................................
21 ..........................................................................
21 .........................................................................

:
28 ..........................................................

28 ................................................................


27 .........................................................................................
21 ......................................................................................................
17 .....................................................................


11...................................................................................
(12 ....................................................................... )Explicit


12 .......................................................................
18 ..........................................................................................
11...........................................................................................
06 ..........................................................................................
01 ....................................................................................
: 01.......................................................................
00 ...........................................................................................
08 ....................................................................................
08 .............................................................
07 ........................................................................
() 01 ..................................................................................
86.......................................................................
6

86..............................................................................
87........................................................................................
81.........................................................................
: 81 ............................................................
81 .................................................................................
81 ............................................
80 ...................................................................................................
80 .......................................................................................
88.............................................................
87.........................................................................................
76................................................................................
76.................................................................................
71 .......................................................................
71 .....................................................
71.................................................
71 .................................................................................
: 78.................................................................
78...............................................................................
71 ..................................................................................
17 ...............................................................................................
: 11 ..................................................................................
11 ......................................................................................... If Then
11......................................................................................... Select Case
11.......................................................................................................
. 11........................................................................
For 767 .................................................................................
Do While 760 ..........................................................................
7

768..............................................................................................................
Do Until 767...........................................................................
761 .................................................................................
: 777 ..................................................................
777 ....................................................................................................
771 .............................................................................................
778 ...............................................................................................
: 771 ................................................................................
711 .................................................................................................
712 .........................................................................
717 .......................................................................
711 ..................................................................................................
711 ..............................................................................
711........................................................................................
: 711 .......................................................................
711 .................................................................................................
: 727 ................................................................................
727 ...........................................................................................
721 ..........................................................................................
721 .............................................................................................................
722 ............................................................................
720.......................................................................
720........................................................................................
728 ...........................................................................................
727 .......................................................................................
711.....................................................................................
710 ............................................................................
8

: 717 .............................................................
717 ...............................................................................................
701 ..............................................................................
700 ........................................................................... VBA
708 ............................................................................
701 ....................................................................................
701 ..............................................................................................
781 ......................................................................................................
712 ............................................................................................... :

11


.
.

soheil.radfar@modares.ac.ir

amir.irandoust@modares.ac.ir
11

12


...

13

14

:

.

0222 0222 .

0222
:

0202 :0202
0202 0202

macro-enabled workbook . macro-enabled

workbook xlsm .
( )standard excel workbook

( )workbook

xlsm . save as
" "Excel Macro-Enabled Workbook .

( )options :
15

macros

without notification

all

Disable
.

all

macros

with notification

Disable
.


macros
signed

macros

all

digitally

Disable

except
.

Enable all macros

Disable all macros with notification


.

:
16


0202 0202

( )file

Trust Center

Macro Settings .

( )options.

Trust Center Settings .

0202 0202
0202 0202

.
.

17

( )Trusted Locations
:

( )file ()options

Trust Center
Trust Center Settings .

Trusted Locations .

Trusted Locations Add new location .

OK .
: My Documents

( ) .

18


...

.
19

21

:
()The excel visual basic editor

Alt+F11 .

):

view .
.

21

()The project window


(

) .

( )

( )Modules

.
.

()The Code Windows


0

( )worksheet1 (
).

.0
.
22

.0 insert module .

()Properties Window

( !) .

( .)...

()The Immediate Window


View Immediate Window

Ctrl+G .

. ?j return

j .

23

()The Locals Window


View Locals Window
.

()The Watch Window


( )watch expressions

View Watch Window


.

. :

.0 .

.0 ( )Debug Menu

( )Quick Watch .

.2 Add .
24

25

26


...

.
27

28

0222 0202 View .


:

.
29

( macro2macro1 )...


. .

)Ctrl+C .

.

0222 0202

31

( )Record Macro

( ).

()Use Relative References


A1 C1

C1

0222 0202 ()Macros Menu

. :

Alt+F8 .


31

( )Run .

. Alt+F11

. :

( )
32

33

34


...

35

36


:
:

.

.

.
.

.
:

.

!
37

()Comments

.

.

()

.
.

38


.
.


39

) .

() .

4 :

()


.
41

( )

. view toolbars edit

edit toolbar .
Comment Block
. Uncomment Block

.
.


.



.


41

.
.

( )space

(_) .
.


. :

:
42

If

43

44


:...


.
45

46


.


. = Pi

3.14159265...
.

sVAT_Rate

. sVAT_Rate
.

.
.

.

47


( )

).


.


( !).

.

.

& % $ # !
.


111 .

.

(

)InterestRate ( )interest_rate .
48

. Next End With Dim Sub : For.

.


.

Workbook : .Range

. (
) Range

Range Range .


Workbook Range

MyWorkbook MyRange .


.

.

49

6 111

Byte

Boolean

true false

Integer

( . -32768 +32768

Long

-2,147,483,648

Single

-3.4e38 +3.4e38

Double

-3.4e38 +3.4e38

Currency

-922,337,203,685,477.5808

Date

Object

String

String 01166

.
.

!)

( . !)

+2,147,483,648

.
.

+922,337,203,685,477.5808

Date 766 -17

1111

.
.

51

Variant

1666666


Double

. String.

... .

. .



.


51


.
.

Variable_Name

Data_Type .
:

Variant .

( )
Variant .

. :
.0 :

Variant

.



52

) . Variant

Integer Single . Variant


.0 :


.

.

sVAT_Rate

VATRate = 0.175 .

sVAT_Rate 27020


VATRate

.



.

.2 :

.

.

!
53

()Explicit


. ( ) :



( Require

)Variable Declaration
:

Tools Options

...

Editor .

Require Variable Declaration OK .

Require Variable Declaration


.

( ) .
.

sVAT_Rate

Total_Cost . sVAT_Rate
.
54

sVAT_Rate



(

).

Explicit

as

Single

As

Double

sVAT_Rate
)(Total_Cost

Option
Dim

Function

.
.
.

sVAT_Rate Total_Cost


(

!) .

Total_Cost

sVAT_Rate

.)End If


.

.

End Function
Explicit
Double

Option
As

as

Single

)(Total_Cost
sVAT_Rate

Function

Dim

.
.
End Function
Explicit
Double

As

.
.

Then

Option

>

)(Total_Cost

Total_Cost

Dim sVAT_Rate as Single

.
.
.

If

.
.

End

End Function

55

Function

If


Dim .

. Public Dim
.

Dim Private

.

Dim Private Const

( !) .

:
Explicit
Public
sVAT_Rate Single

as

sVAT_Rate

Option
Public

iMax_Count Public Const iMax_Count = 5000 .


.

.
.

Explicit
Private
sVAT_Rate Single

as

sVAT_Rate

Option

Private

iMax_Count Private Const iMax_Count = 5000 .


.

.
.

56


(
)
.

Const

. .

.
( )
( ) .

.
( )

).


57

Public .

.

.

(

!).

( )xlCalculationManual
( . ).

xlCalculationManual
. Help :

xlCalculationAutomatic

-4105

xlCalculationManual

-4135

xlCalculationSemiautomatic

58

xlCalculationManual -4135 .

Immediate
:

Immediate Ctrl+G .

Print.


( )
. .

60006 .
.

.

0 262

( !).

Dim


) .
(
MyString
02 ( . *
60006 YourString ).
59


999

. Date

( )

.

.

Date -0

2022 -20 9999 . 02222


.

(
).

:

)#( hash .
61

( 00

04) .
Control Panel
.

)mm/dd/yyyy . ( !)

MyDate ( mm/dd/yyyy
):


( )MsgBox

MyDate .
dd/mm/yyyy
MyDate 11/10/2013
.

61

62


:...

.
63

64

. .

02
02

:


02

. :

65



(

) . 02

02
.

A 0 :

02
02
. 0222

.

.

.
00
.


MonthNames
) MonthNames(1 ) MonthNames(2

66


.

.

. Dim Public


(
.
) . :

.
. .
0
. 0
20 . :

Jan_Sales_Figures

0 -00 :

67



.
:

Team_Members 02
0 02 .

2 09 :

2 ( )0

02

2 09 .
.

,
:

68

2 20
2 0 20 6
.

()
.
.


.

. :

ReDim :

ReDim

69

.
Preserve

Preserve

).

( ) .

.
(

).

71

( "_ ")

. 22

.
.

GoTo .
( ) ( ),

71

72


:...

73

74


Alt+F11 F2 .

VBA .

F1 .

.
75

( )

( )
.

i .
76

IsMissing .
:

( :)ByVal

( )

( :)ByRef
.
77

ByVal ByRef

i )Sub AddToCells(ByVal i As Integer


.
.
.
.

End Sub

i )Sub AddToCells(ByRef i As Integer


.
.
.
.

End Sub

ByVal ByRef
.
.



. :

78

( )

. :

()header
.

) 7 1

Double . Double
:

79

Double

( As Double ) .

.

A2 A1 A3 SumMinus

. :

) 0

81

) 2 ( )Bold

0
.

81

( )
.

0 ( ).

Alt+F8 .

Options ... .
OK .


( .)Ctrl+C

()

Public Private .
:
82


)Public Sub AddToCells(i As Integer

Public .

.
.
.

End Sub

)Private Sub AddToCells(i As Integer



Private .
. .
.

End Sub

Public (
)
Private .

Exit Function Exit Sub .



. .

83

84


...


.
85

86

:
:

.
.

.
.

.
.

.
87

.
.
.

-7

-1

Help .

...

Activate
BeforeClose

BeforeSave

BeforePrint

Deactivate

NewSheet

Open

SheetActivate
SheetBeforeDoubleClick

SheetBeforeRightClick

SheetChange

SheetDeactivate
88

SheetSelectionChange

WindowDeactivate

WindowActivate

...

Activate

BeforeDoubleClick

BeforeRightClick

Change

Deactivate

SelectionChange

A .

WorksheetChange . .
A

B .

89

Worksheet_Change

. .

( )
.



.
.

-7 .

-1 Alt+F11 .

-1
( .

ThisWorkbook .

Worksheet ).Sheet1

-2
.

91


...


.
91

92

:
VBA If Then Select Case .

If Then
If Then

.
If Then :

ElseIf Else
93

( ) .

) 0 Time 270 Good Morning

Good Afternoon .

) 0 If Then
:

) 2 :ElseIf
.

If :

94

) 4
:

) 0 ElseIf

Select Case
If Then
.

:Select Case

95

Case Else ( )

) 6 .

96

) 2 Select Case
:

Select Case

. :

Case Is <= 5

Case Is

Case 6,7,8,9

Case 10

Case Else


Select Case
(

).
97

) 8 Select Case .
:

-0

-0

-2

98

.
99

GoTo

.
GoTo

GoTo :

InputBox .
111

: Steve Ballmer

WrongName .
Steve Ballmer
). ).

Exit Sub MsgBox


. .

For

Do While
Do Until

For
For . For Next .For Each
For Next


VBA . :

i 02...200

iArray Total .

0
. .

Step . :
111

For 270 d
272 270 270 272 ... 979 0272 VBA

.
:

-0

0298...0 i .

) 0 Cnt 0 0222 (

0 ). Total Total :

) 0 0 2

112

) 2 For-Next .

113

For Each


For Each .

For Each Worksheet :

) 4

114

) 0

) 6 :

Exit For

dVal 022
:

) 2 ( )Str
115

KBR89Z KBR :

Do While

.
Do While

0222 .

iFib_Next < 1000 .


iFib_Next 0222 .

116

Do While
.
.

iFib_Next
.

iFib_Next

While .
117

Do Until

Do While .

. Do Until

A :

)) IsEmpty(Cells(iRow,1 Do Until

Do While

.
:

118


...

119

111

:


. :


. .

/
\

( =0=0 )


111

( . 2\4

Mod

)0

( =0=0 )

0
0

& :

&

( A & B .
)AB

( True )False

. :

<>

<

=
>

=>

=<

112

A And B

And

A B .

A Or B

Or

A B .
.

Not A

Not

A
A .



. :

Abs

Chr

Date

DateAdd

Abs(-20) == 20
Abs(20) == 20

Chr(10) == line break

Chr(97) == a


113

DateAdd(Interval,Number,Date) :

Interval
:
Interval

yyyy

ww

) 20 :DateAdd(d,32,01/01/2009 0229/20/20

DateDiff

Day
Hour
InStr

0229/20/20 .

.:

) :DateDiff(d,01/01/2009,02/02/2009

.20
.

.:

)" :InStr(1, "Here is the searchword", "searchword 02


( . ).
114

Int

IsDate

True

IsError

True

IsMissing

IsNumeric

True .

Left

Len
Month
Mid

False

False .

True .

) Left("abcdefghijklm", 4 abcd .

Mid(String, Start, Length) :

) Mid("abcdefghijklm", 4, 5 defgh
0 .

Minute

Right

Now

Second

Time

Sqr

Ubound

Year

115

116


...
Object

.
.

.
117

118

:
.

: .
.

...

" " .

VBA "" .
.

( )
.

( ) .

) ( ) .
119

Workbooks

Workbook

))Workbooks(Book1

Workbooks(1),

( .Workbooks(1),

) .)Workbooks(Book1
ActiveWorkbook


( )

Sheets


.
.

)"Sheets(1) or Sheets("Sheet1

121

Worksheets

.
.

Worksheet

)"Worksheets(1) or Worksheets("Wksheet1

)"Sheets(1), Worksheets(1), Sheets("Sheet1

)"or Worksheets("Wksheet1

ActiveWorksheet
.

Rows

Columns

)Rows(1

)Columns(1

121

Range

)Worksheet.Cells(1,1

.
OR

OR

)""B10

)"Worksheet.Range("A1:B10
Worksheet.Range("A1",

))Worksheet.Range(Cells(1,1), Cells(10,2

)"Workbooks("WB1").Worksheets("WS1").Range("A1:B10

WB1 .

)"Worksheets("WS1").Range("A1:B10

WS1
:
122

)"Range("A1:B10

( )

( ) .
:

.0 (" ") :

.0 :

.2
:

.4 .2 :

.0 .D :

.6
:

123

: .

C2 0 :

82 ( A1 0 )0

( H10 02 )8 .

02 8 99 .

124

:
.

C2 :

0 0 .

.
:

. -

VBA .

A1 0 :

002 :
125

20 :

B1 :

$12.3 $12.3 :

12.3 :

:
126

F3 :

2 F3 :

. -

. (" ")

A :

127

GoTo . :

Copy Paste

A1:A12 paste C1 :

Clear

Delete

. Delete

delete .
128

Delete
.

delete :

ActiveWorkbook,ActiveSheet,ActiveWorksheet .


Select :

Worksheets("Data").Select
Range("A1", "B10").Select


VBA

Set :

Dim DataWb As Workbook

)Set DataWb = Workbooks.Open("C:\Data.xls", False, True

)0 VBA For Each .


(

) .

129

) 0 .


. Set
.

) 2

.
.
131

Set

131

132

...

133

134

:

.
.

Sheet

Activate .

( )sheet1 Activate

for Sheet1 .


VBA ( Alt + F11 ). .
:

135

VBA Editor

. -

VBA ( )
136

.
( )

) 0 B1 .

Selection_Change

. Target

Selection_Change

()Target

Worksheet_Selection_Change
B1 .

137

138

...

.

.
139

141

:
VBA 2 . :
( )Compile ( .)Bug
.
.


VBA Compiler
.

VBA .

) .

VBA
.

Compile error: Variable not defined


( .

Option Explicit ).

141

.Run-time error 11: Division by zero :

VBA

( )Debug( ) . Debug
VBA VBA .

142

VBA .
: (

.)View => Local Window

VBA .

143

VBA .

( )crash.
VBA Resume On Error
.

VBA ( ) .
VBA
. :

144

Data

Data .

Ok .
Exit Sub

.
145

On Error
:

On Error

On Error GoTo

VBA

( ):
VBA

On Error GoTo 0

VBA

On Error

On Error Resume Next

VBA


.

. .
146

Resume .

.
Resume :
Resume

Resume

Resume Next

Resume label


bug VBA
.

VBA

( ).

147

-0 :

-0 :

( ).
-2


.
-4

. .

-0

-6

: .


Debug.Print


) 0 CellCount :

148

CellCount .
) 0 .
:

& .

.
vbNewLine .

. .

) 2 .

) 4 Ctrl + Break

:
149

. :

Continue
End

Debug VBA ( .

).

Help
Ctrl + Break .

Debug

.
VBA ()VBE

ok .

VBA

F9
.


151

Toggle Breakpoint
.

.
.

Break Mode

. ] [break VBE .
F5 Run Sub/UserForm

VBE .

F9 .
Ctrl+Shift+F9 .

151

VBA
:

.
:

VBA
. :

VBA ( . ).
F8 .
152

(
).

. ( ).

F8 . .
.

Ctrl+G


. CellCount

Enter:


.
.

VBA.

153

154

F8 .
.

155


...

.
156

157

( )Range objects :
VBA O select .

O .

O ( )recorder .
recorder

O .
) Range(D45 ) Range(Total .
40

D46 .

FormulasDefined NamesDefine Name

.
( ) .
158

O .

Ctrl .
.
:

recorder A1:A5 B1:B5


:

recorder .

Ctrl+Shift+ Ctrl+Shift+
.

CurrentRegion .

VBA .
.
159

: Shift

End .

EntireColumn

EntireRow .

Clipboard paste

record recorder :

Cut

161

For-Next .
.
. Cell

. For Each-Next
.

.
( 072487026) -
. .

SpecialCells . Set

161


. SpecialCells

SpecialCells :

HomeEditingFind & SelectGoTo

( )

UsedRange -
162

. Intersect
.

WorkRange

WorkRange
.

InputBox
. .

A1

163

Cancel InputBox

A1 .
. Cancel .

: ) (x
. ( Cancel )

x A1 . .

x Varient
( Cancel ) .

.
( ) . TypeName

164

MsgBox Range .
If .


Ctrl
. .

.
:

:
165

Areas

. Count

VBA
VBA .
.

. :

166

.
:


( ) True

:
. : VBE

) (Immediate :

.
:

:
167

.

.

-
.

: :

Delete ( ).

DisplayAlerts True

.
:

168

Set .
Rate :

Rate

. InterestRate :

. .
MyVar
169

. .

:

. .


. :

With-End With

With-End With .

With-End With :

recorder With-End With .

171

171


...

172

173

) 0 ()

For if

174

)0 Do While .
if.

175

)2 A
. .

176

)4 A Sheet2

A
. .

177

)0 Selection Change
.

B1
.VBA

178

)6 On Error Resume .

179

) 2 VBA

VBA
.


.
Left Top . Left

Top


stop . reset

)(Private Sub StartButton_Click


repeat:

With VBAProject.Sheet1.Image1
.Left = .Left + 1
DoEvents

If .Left > 200 Then .Left = 1


End With

GoTo repeat
End Sub

181

Left Top

181

Top Left :

) 8

02 .

.C

182

EntireColumn EntireRow .
) 9

02) . 0 0 2 !DIV/0

. ! DIV/0

() .

.
.

183

) 02/

B A C

-0

-0 . .

) 00 A
A :

.
. With..End With .

0 .
.
184

) 00

.C

) 02

0 . .

185

) 04

Rng

Rng
.

Rng .B2:D10

) 00

( )

( True .)False

186

) 06 ()

) IsNumeric(value ()True

) Application.IsNumeric(value ( )True
.

) Application.IsText(value ( )True
() .

187

) 02 ( )Resolution
:

) 08

DateDiff .

) 09

X
. Outlook

Exchange Lotus Notes VIM MAPI

) Banyan VINES MHS(Novell) SMTP/POP3(Internet mail Active Messaging

188

) 02

WhileWend

. :

189

DoLoop :

191

) 00

191

) 00

H2

192

Rows.Count .
End ) End(xlUp .

193

:
[1] John Walkenbach, Excel VBA Programming for dummies, Wiley (2013)

[2] Peter G. Aitken, Excel Programming, Weekend Crash Course, Wiley (2003)

[3] 1200 macro examples, Release 5, February 19, 2008, Add0ins.com LLC
[4] Curtis D. Frye, Microsoft excel 2013 step by step, Microsoft (2013)

[5] Excel Functions tutorials


[6] Excel VBA tutorials

194

You might also like