VBS基础篇

OptionExplicit
'*********************************Date/Time函数*******************************
'CDate函数把一个合法的日期和事件表达式转换为Date类型,并返回结果
Dim d1
Dim d2
Dim d3
d1="April 22,2001"
IfIsDate(d1) Then
    MsgBoxCDate(d1)
EndIf

d2=#2/22/01#
IfIsDate(d2) Then
    MsgBoxCDate(d2)
EndIf

d3="3:18:40 AM"
IfIsDate(d3) Then
    MsgBoxCDate(d3)
EndIf

'Date函数返回当前系统的日期
'日期
MsgBoxDate
'日期+时间
MsgBox Now
'时间
MsgBox Time

'DateAdd函数可返回已添加指定时间间隔的日期
MsgBoxDateAdd("yyyy",1,"31-Jan-2003")'加一年
MsgBoxDateAdd("yyyy",1,"31-Jan-2003")'减一年
MsgBoxDateAdd("q",1,"31-Jan-2003")'加一个季度
MsgBoxDateAdd("m",1,"31-Jan-2003")'加一个月
MsgBoxDateAdd("y",1,"31-Jan-2003")'当年的第几天
MsgBoxDateAdd("d",1,"31-Jan-2003")'增加一天
MsgBoxDateAdd("w",1,"31-Jan-2003")'当周的第几天
MsgBoxDateAdd("ww",1,"31-Jan-2003")'增加一个周
MsgBoxDateAdd("h",1,"31-Jan-2003")'某日期的第一个小时
MsgBoxDateAdd("n",1,"31-Jan-2003")'某日期的第一分钟
MsgBoxDateAdd("s",1,"31-Jan-2003")'某日期的第一秒

'DateDiff函数可返回两个日期之间的时间间隔数
MsgBoxDateDiff("yyyy",Date,"31-Jan-2012")
MsgBoxDateDiff("q",Date,"31-Jan-2012")
MsgBoxDateDiff("m",Date,"31-Jan-2012")
MsgBoxDateDiff("y",Date,"31-Jan-2012")
MsgBoxDateDiff("d",Date,"31-Jan-2012")
MsgBoxDateDiff("w",Date,"31-Jan-2012")
MsgBoxDateDiff("ww",Date,"31-Jan-2012")
MsgBoxDateDiff("h",Date,"31-Jan-2012")
MsgBoxDateDiff("n",Date,"31-Jan-2012")
MsgBoxDateDiff("s",Date,"31-Jan-2012")

'DatePart函数可返回给定日期的指定部分
Dim d
d="2/10/2012 16:25:56"
MsgBoxDatePart("yyyy",d)
MsgBoxDatePart("m",d)
MsgBoxDatePart("q",d)
MsgBoxDatePart("w",d)
MsgBoxDatePart("ww",d)
MsgBoxDatePart("y",d)
MsgBoxDatePart("s",d)
MsgBoxDatePart("h",d)
MsgBoxDatePart("n",d)
MsgBoxDatePart("d",d)

'DateSerial函数可返回指定的年、月、日的子类型Date的Variant
MsgBoxDateSerial(2012,2,30)

'DateValue函数返回一个日期类型
MsgBoxDateValue("31-Jan-2012 2:39:49 AM")

'Day函数可返回介于1到31之间的一个代表月的天数的数字
MsgBoxDay(Date)

'FormatDateTime函数可格式化并返回一个额合法的日期或时间表达式
MsgBoxFormatDateTime(Now,0)
MsgBoxFormatDateTime(Now,1)
MsgBoxFormatDateTime(Now,2)
MsgBoxFormatDateTime(Now,3)
MsgBoxFormatDateTime(Now,4)

'Hour函数可返回介于0到23之间的代表天的小时数的数字
MsgBoxHour(Now)

'IsDate函数可返回一个布尔值,指示经计算的表达式是否可被转换为日期,如果表达式是日期或可被转换为日期,则返回True,否则,返回False。
MsgBoxIsDate("15-3-2012")

'Minute函数可返回表示小时的分钟数的数字
MsgBoxMinute(Now)

'Month函数可返回表示年的月份的数字
MsgBoxMonth(Now)

'Second函数可返回表示分钟的秒数的数字
MsgBoxSecond(Now)

'Time函数可返回当前的系统时间
MsgBox Time

'Timer函数可返回午夜12时以后已经过去的秒数
MsgBox Timer

'TimeSerial函数可把时、分、秒合并成为时间
MsgBoxTimeSerial(3,45,50)

'TimeValue函数可返回包含时间的日期子类型的变量
MsgBoxTimeValue(Now)

'Weekday函数可返回表示一周的天数的数字,介于1和7之间。
MsgBoxWeekday(Date,0)

'WeekdayName函数可返回一周中指定一天的星期名
MsgBoxWeekdayName(3)
MsgBoxWeekdayName(Weekday(Date,1))

'Year函数可返回表示年份的一个数字
MsgBoxYear(date)

'*********************************Conversion函数*******************************
'Asc函数可把字符串中的第一个字母转换为对应的ANSI代码,并返回结果
MsgBoxAsc("A")
MsgBoxAsc("a")

'CBool函数可把表达式转换为布尔类型
Dim a
a=5
MsgBoxCBool(a)

'CByte函数可把表达式转换为字节类型
Dim b
b=134.12345
MsgBoxCByte(b)

'CCur函数可把表达式转换为货币类型
Dim c
c=12345.12345656
MsgBox CCur(c)

'CDbl函数可把表达式转换为双精度类型
Dim e
e=123.12454656577435235436
MsgBoxCDbl(e)

'Chr函数可把指定的ANSI字符代码转换为字符
MsgBoxChr(65)

'CInt函数可把表达式转换为整数类型
MsgBoxCInt(12.3435)

'CLng函数可把表达式转换为长整形类型
MsgBoxCLng(12.676)
MsgBoxCLng(12.243)

'CSng函数可把表达式转换为单精度类型
MsgBoxCSng(122.355465)
MsgBoxCSng(21.23222)

'CStr函数可把表达式转换为字符串类型
MsgBoxCStr(false)
MsgBoxCStr(Date)
MsgBoxCStr(Empty)
MsgBoxCStr(Error)


'*********************************Format函数*******************************

'FormatCurrency函数可返回作为货币值被格式化的表达式,使用系统控制面板中定义的货币符号
MsgBoxFormatCurrency(21.23456)

'FormatNumber函数可返回作为数字被格式化的表达式
MsgBoxFormatNumber(12.23456775,3)

'FormatPercent函数可返回作为百分数被格式化的表达式
MsgBoxFormatPercent(3/7,3)

'*********************************Math函数*******************************
'Abs函数可返回指定的数字的绝对值
MsgBoxAbs(-9)

'Atn函数可返回指定数字的正切
MsgBoxAtn(9)

'Cos函数可返回指定数字的余弦
MsgBoxCos(9)

'Exp函数可e的幂次方
MsgBoxExp(2.3)

'Fix函数可返回指定数字的整数部分
MsgBoxFix(-6.325)
MsgBoxInt(-6.325)

'Log函数可返回指定数据的自然对数
MsgBoxLog(10)

'Rnd函数可返回一个随机数,数字总是小于1但大于或等于0.
MsgBoxRnd

'Sgn函数可返回指定数字的符号的整数
MsgBoxSgn(15)
MsgBoxSgn(-5)
MsgBoxSgn(0)

'Sin函数可返回指定数字的正弦
MsgBoxSin(9)

'Sqr函数可返回一个数的平方根
MsgBoxSqr(9)

'Tan函数可返回指定数字的正切
MsgBoxTan(9)


'*********************************Array函数*******************************
'Array可返回一个包含数组的变量
Dim Arr
Arr=Array(1,2,3,4,5)
MsgBox Arr(0)

'Filter函数可返回一个基于0的数组,此数组包含以特定过滤条件为基础的字符串数组的子集
Dim Arr1(5),b
Arr1(0)="Saturday"
Arr1(1)="Sunday"
Arr1(2)="Monday"
Arr1(3)="Tuesday"
Arr1(4)="Wednesday"
b=Filter(Arr1,"n",True)
MsgBox b(0)
MsgBox b(1)
MsgBox b(2)

'ISArray函数可返回一个指示指定的变量是否为数组的布尔值。如果变量为数组,则返回True,否则返回False。
Dim a(3)
a(0)=1
a(1)=2
a(2)=3
MsgBoxIsArray(a)

'Join函数可返回一个由某个数组中一系列子字符串组成的字符串
Dim a(5),b
a(0)="Saturday"
a(1)="Sunday"
a(2)="Monday"
a(3)="Wendesday"
a(4)="Tuesday"
b=Filter(a,"n",True)
MsgBoxJoin(b,",")

'LBound函数可返回指示数组维数的最小下标。(始终为0)
'UBound函数可返回指示数组维数的最大下标。
Dim a(5)
MsgBoxLBound(a)
MsgBoxUBound(a)

'Split函数可返回基于0的一维数组,此数组包含指定的子字符串
Dim txt,a
txt="hello/world!"
a=Split(txt,"/")
MsgBox a(0)
MsgBox a(1)

'*********************************String函数*******************************
'InStr函数可返回一个字符串在另一个字符串中首次出现的位置
Dim txt,pos
txt="This is a beautiful day!"
pos=InStr(4,txt,"is",1)
MsgBox pos

'InStrRev函数可返回一个字符串在另一个字符串中首次出现的位置,搜索从字符串的末端开始,但是返回的位置是从字符串的起点开始计数的。
Dim txt,pos
txt="This is a beautiful day!"
pos=InStrRev(txt,"i",-1,1)
MsgBox pos

'LCase函数可把指定的字符串转换为小写
'UCase函数可把指定的字符串转换为大写
Dim txt,pos1,pos2
txt="This Is A Beautiful Day!"
pos1=LCase(txt)
pos2=UCase(txt)
MsgBox pos1
MsgBox pos2

'Left函数可从字符串的左侧返回指定数目的字符
'Right函数可从字符串的右侧返回指定数目的字符
Dim txt,pos1,pos2
txt="This is a beautiful day!"
pos1=Left(txt,5)
pos2=Right(txt,6)
MsgBox pos1
MsgBox pos2

'Len函数可返回字符串中字符的数目
Dim txt,pos
txt="This is a beautiful day!"
pos=Len(txt)
MsgBox pos

'LTrim函数可删除字符串左侧的空格
'RTrim函数可删除字符串右侧的空格
'Trim函数可删除字符串两端的空格
Dim txt,pos1,pos2,pos3
txt="  This is a beautiful day!  "
pos1=LTrim(txt)
pos2=RTrim(txt)
pos3=Trim(txt)
MsgBox pos1
MsgBox pos2
MsgBox pos3
MsgBoxLen(pos1)
MsgBoxLen(pos2)
MsgBoxLen(pos3)

'Mid函数可从字符串中返回指定数目的字符
Dim txt
txt="This is a beautiful day!"
MsgBoxMid(txt,1,5)
MsgBoxMid(txt,2,5)
MsgBoxMid(txt,1)
MsgBoxMid(txt,5)

'Replace函数可使用一个字符串替换另一个字符串指定的次数
Dim txt
txt="This is a beautiful day!"
MsgBoxReplace(txt,"is","AA",4,2)

'Space函数可返回一个由指定数目的空格组成的字符串
Dim txt
txt=Space(20)
MsgBox txt
MsgBoxLen(txt)

'StrComp函数可比较两个字符串,并返回表示比较结果的一个值
Dim txt1,txt2
txt1="hello"
txt2="hell"
MsgBoxStrComp(txt1,txt2)

'String函数可返回包含指定长度的重复字符的一个字符串
MsgBoxString(10,"*")

'StrReverse函数可反转一个字符串
Dim txt
txt="This is a beautiful day!"
MsgBoxStrReverse(txt)

'*********************************其他函数*******************************

'InputBox函数可显示一个对话框,用户可在其中输入文本或点击一个按钮。
Dim fname
fname=InputBox("Enter your name:")
MsgBox ("Your name is "&fname)

'IsEmpty函数可返回指定的变量是否被初始化的布尔值。
Dim x,y
y=10
MsgBox IsEmpty(x)
MsgBox IsEmpty(y)

'IsNull函数可返回指定表达式是否无效数据的布尔值
Dim x,y,z
x=Null
y=10
z=""
MsgBox IsNull(x)
MsgBox IsNull(y)
MsgBox IsNull(z)

'IsNumeric函数可返回指示指定的表达式是否可作为数字来计算的布尔值。
Dim x
x=10
MsgBoxIsNumeric(x)
x=Empty
MsgBoxIsNumeric(x)
x="1 h"
MsgBoxIsNumeric(x)
原文地址:https://www.cnblogs.com/wakey/p/5798106.html