You are on page 1of 0

40

: 3 VBA
VBA . VBA )
( VBA .
. .
VBA .

I / O VBA
.
. )(MsgBox
. )( MsgBox .
. )( InputBox .
.

) (.
. .

)(MsgBox
)( MsgBox .
Cancel Yes OK .
. 1,3 .

: 1 . 3

)( MsgBox :
)]MsgBox(prompt[, buttons] [, title] [, helpfile, context

) ( title Buttons Prompt helpfile Context .


prompt .
. .

41
helpfile context . button VBA
. VBA 1,3 .
: 1 . 3 button )(MsgBox

VbOKOnly

OK .

VbOKCancel

OK Cancel .

VbAbortRetryIgnore

Abort , Retry , Ignore

VbYesNoCancel

Yes , No , Cancel .

VbYesNo

Yes , No .

VbRetryCancel

Retry , Cancel .

VbCritical

Critical Message .

VbQuestion

Warning Query .

VbExclamation

Warning Message .

VbInformation

Information Message .

vbDefaultButton

vbDefaultButton

vbDefaultButton

vbDefaultButton

VbApplicationModal

VbSystemModal

VbMsgBoxHelpButton

Help .

VbMsgBoxSetForeground

VbMsgBoxRight

VbMsgBoxRtlReading

) ( .

http://amar80.blogfa.com

42

Dim msgString As String


Dim retValue As Integer
"msgString = "An error occurred!" & vbCrLf & "Do you wish to continue
)"retValue = MsgBox(msgString, vbCritical + vbYesNo, "Continue

Yes No .
) Yes .( No )( MsgBox
. VBA ) . 2,3 (.
.
: 2 . 3 )(MsgBox

VbOK

OK

VbCancel

Cancel

VbAbort

Abort

VbRetry

Retry

VbIgnore
VbYes
VbNo

Ignore
Yes
No

Yes 6 retValue .
6 ) . 5 (.

. .
.
.

)(InputBox
. )(InputBox
2,3 .

43

: 2 . 3

)( MsgBox Prompt .
)]InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context

.
xpos ypos help .
Dim retValue As String
)"retValue = InputBox("Enter your name.", "Name", "First, Last

prompt
2,3 . title prompt Default
. .

)( InputBox .

)( InputBox .
Cancel ) " " (.

Type Conversion
.
) 3,3 (.
: 3 . 3

)(CBool
)(CByte
)(CCur
)(CDate

Boolean
Byte
Currency
Date

) CBool( True
) CByte(
) CCur(
) CDate( // :: PM.

http://amar80.blogfa.com

44

)(CDbl
)(CDec
)(CInt
)(CLng
)(CSng
)(CStr
)(CVar

Double
Decimal
Integer
Long
Single
String
Variant

) CDbl( ^ E+
) CDec( ^
) CInt( *
) CLng( ^
) CSng( *
) CStr( ""


)( CInt . :
val =
val =
)val = CInt(val * val

)( CInt . Val3 Integer .


)( CInt .

) ) ( CInt( 33000
.

VBA
. VBA
) . 4,3 (.
. . )( ) Atn
( ) ( . VBA .
) ( .
: 4 . 3 VBA

)(Abs

) Abs(-10 10

)(Atn

) (

) 4 * Atn(1 3,14159265358979

)(Cos

) Cos(0 1

45

)(Exp

) Exp(1 2,71828182845905

)(Fix

) Fix(-9,1 -9

)(Int

) (

) Int(-9,1 -10

)(Log

) Log(2,718281828 1

)(Rnd

0 1

)(Sgn

) (

) Sgn(-9 -1

)(Sin

) Sin(3,14159265/2 1

)(Sqr

) Sqr(9 3

)(Tan

) Tan(3,14159265/4 1

Dim pi As Double
)pi = * Atn(

)( Rnd . )( Rnd 0 1 .
. )( Rnd
. . Randomize
.
Dim val
Randomize
)val = Int( * Rnd +

1 100 .


.
VBA
. .
. ) . 5,3
(.

http://amar80.blogfa.com

46
: 5 . 3 VBA

)(Str

) Str(12 12

)(Val

)" Val("5XJT 5

LTrim(),
RTrim(),
)(Trim
)(Left

)" Trim("My String "My String".

) Left("Test",2 "Te".

)(Right

) Right("Test",2 "st".

)(Mid

) Mid("Test", 22 "es".

)(InStr

)" InStr("Test", "e 2.

)(Len

)" Len("Test 4.

)(LCase

)" LCase("ABCD "abcd".

)(UCase

)" UCase("abcd "ABCD".

)(StrConv

) Debug.Print StrConv("fred", vbProperCase

"Fred".

)(StrComp

)Debug.Print StrComp(str1,str2,vbTextCompare

)(StrReverse

)" StrReverse("Test "tseT".

)(Format

)"<" Format("ABCD", "abcd".

)(Space

0 -1
1 .


)(Asc

)" Asc("A 65.

)(Chr

) Chr(65 "A".

)(Dir

)" Dir("C:\Windows\*.ini

)( CurDir

)(CurDir

47


.
( Format() , StrConv() , UCase() , LCase() ) .
.
)"Cells(, "A").Value = LCase("ALL LOWERCASE
)"<" Cells(, "A").Value = Format("ALL LOWERCASE ",
)Cells(, "A").Value = StrConv("ALL LOWERCASE ", vbLowerCase

3,3 .
)( Format() , StrConv() , LCase . . )( Format
.

: 3 . 3

Val() : )(Str
VBA ) ( + ) & ( . ) ( +
) & ( . ) & (
"" newstr .
"string = "
"string = "
http://amar80.blogfa.com

48
newStr = string & string

)( Str )( Val . VBA


. )( Val num1
VBA . )( Val
.
num =
"str = "
)num = num + Val(str

)( Str .
zip =
zipx =
)newzip = Str(zip) + Str(zipx

)( Str . )( Str
. .

)( Asc )(Chr
)( Chr )( Asc ) 6,3
(. 256 .
VBA . .
)( Chr . :

Range(Chr( + I) & "").Value

: 6 . 3

Character

Character

VBA

][space
!
"
#
$
%
&

'
(
)
*
+
,
-

Character

backspace
tab
linefeed
carriage
return

49

Character

Character

Character

.
/

:
;
<
=
>
?
@
A
B
C
D
E
F
G
H

I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
[
\
]
^
_
`
a
b
c

d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
{
|
}
~

I=0 A1 65 A .
I 0 25 ( A-Z ).
)( Asc . num1
65 :
)"num = Asc("A

6 VBA .

http://amar80.blogfa.com

50

) (
. .
:
_ cell = Mid(rangeStr, InStr(, rangeStr, ":") + ,
))"Len(rangeStr) - InStr(, rangeStr, ":


. rangeStr " "A1:C10 Cell2 C10
. )( Len )( Instr )( Mid .
)( Instr )( Mid . )( Len )( Instr )(Mid
.
.

VBA
VBA .
. . 7,3
.
:7 . 3 VBA

Time

Now

Date

)(DateDiff

)(DateAdd

)(Year

)(Month

1 12

)(Weekday

1 7

)(Day

1 31

)(Hour

0 23

51

)(Minute

0 59

)(Second

0 59

)(TimeSerial

)(DateSerial

)(TimeValue

)(DateValue

Now Time Date ) . :


( .
. )( Hour() Day() Weekday() Month() Year
)( Minute )( Second .
. )( Month
. Now )( Month .
)curMonth = Month(Now
'returns current month as an integer
)"prevMonth = Month("Feb ,
'returns

7,3 .
)( TimeValue() DateSerial() TimeSerial )( DateValue
.
)"TimeValue(":
'returns :: AM
)"DateValue("Nov,
'returns //
)TimeSerial(, ,
'returns :: AM
)DateSerial(, ,
'returns //

=
=
=
=

myTime
myDate
myTime
myDate

)( DateDiff .
. s ) . m yyyy
(.
)"myInterval = DateDiff("s", Now, "//
)"myInterval = DateDiff("m", Now, "//
)"myInterval = DateDiff("yyyy", Now, "//

http://amar80.blogfa.com

52
10 2003 .
.

VBA
.
300
. ) . A (.
Insert Function .

: 4 . 3 Insert Function

VBA )( PI .
)( SORT VBA . :
)myAvg = Application.WorksheetFunction.Average(, ,
'returns
)mySum = Application.WorksheetFunction.Sum(, ,
'returns

worksheetFunction Application .
. Application 7
.

)( MsgBox )( InputBox VBA


. .

53
. VBA
. .

: 4 VBA
:5
:6 VBA
:7 VBA
:8 I / O VBA
:9
: 10
:11 VBA
: 12 HTML
: 13
: 14
: 15 Pear Tree Used Cars
: 16 VB VBA
: 17 Windows API VBA
: 18 Stock Ticker
: 19
: 20 VBA
: 21 Excel - VBA
: A
: B XHTML / HTML
: C SQL

http://amar80.blogfa.com

You might also like