Option Explicit '*********************************Date/Time函數******************************* 'CDate函數把一個合法的日期和事件表達式轉換為Date類型,並返回結果 Dim d1 Dim d2 Dim d3 d1="April 22,2001" If IsDate(d1) Then MsgBox CDate(d1) End If d2=#2/22/01# If IsDate(d2) Then MsgBox CDate(d2) End If d3="3:18:40 AM" If IsDate(d3) Then MsgBox CDate(d3) End If 'Date函數返回當前系統的日期 '日期 MsgBox Date '日期+時間 MsgBox Now '時間 MsgBox Time 'DateAdd函數可返回已添加指定時間間隔的日期 MsgBox DateAdd("yyyy",1,"31-Jan-2003")'加一年 MsgBox DateAdd("yyyy",1,"31-Jan-2003")'減一年 MsgBox DateAdd("q",1,"31-Jan-2003")'加一個季度 MsgBox DateAdd("m",1,"31-Jan-2003")'加一個月 MsgBox DateAdd("y",1,"31-Jan-2003")'當年的第幾天 MsgBox DateAdd("d",1,"31-Jan-2003")'增加一天 MsgBox DateAdd("w",1,"31-Jan-2003")'當周的第幾天 MsgBox DateAdd("ww",1,"31-Jan-2003")'增加一個周 MsgBox DateAdd("h",1,"31-Jan-2003")'某日期的第一個小時 MsgBox DateAdd("n",1,"31-Jan-2003")'某日期的第一分鍾 MsgBox DateAdd("s",1,"31-Jan-2003")'某日期的第一秒 'DateDiff函數可返回兩個日期之間的時間間隔數 MsgBox DateDiff("yyyy",Date,"31-Jan-2012") MsgBox DateDiff("q",Date,"31-Jan-2012") MsgBox DateDiff("m",Date,"31-Jan-2012") MsgBox DateDiff("y",Date,"31-Jan-2012") MsgBox DateDiff("d",Date,"31-Jan-2012") MsgBox DateDiff("w",Date,"31-Jan-2012") MsgBox DateDiff("ww",Date,"31-Jan-2012") MsgBox DateDiff("h",Date,"31-Jan-2012") MsgBox DateDiff("n",Date,"31-Jan-2012") MsgBox DateDiff("s",Date,"31-Jan-2012") 'DatePart函數可返回給定日期的指定部分 Dim d d="2/10/2012 16:25:56" MsgBox DatePart("yyyy",d) MsgBox DatePart("m",d) MsgBox DatePart("q",d) MsgBox DatePart("w",d) MsgBox DatePart("ww",d) MsgBox DatePart("y",d) MsgBox DatePart("s",d) MsgBox DatePart("h",d) MsgBox DatePart("n",d) MsgBox DatePart("d",d) 'DateSerial函數可返回指定的年、月、日的子類型Date的Variant MsgBox DateSerial(2012,2,30) 'DateValue函數返回一個日期類型 MsgBox DateValue("31-Jan-2012 2:39:49 AM") 'Day函數可返回介於1到31之間的一個代表月的天數的數字 MsgBox Day(Date) 'FormatDateTime函數可格式化並返回一個額合法的日期或時間表達式 MsgBox FormatDateTime(Now,0) MsgBox FormatDateTime(Now,1) MsgBox FormatDateTime(Now,2) MsgBox FormatDateTime(Now,3) MsgBox FormatDateTime(Now,4) 'Hour函數可返回介於0到23之間的代表天的小時數的數字 MsgBox Hour(Now) 'IsDate函數可返回一個布爾值,指示經計算的表達式是否可被轉換為日期,如果表達式是日期或可被轉換為日期,則返回True,否則,返回False。 MsgBox IsDate("15-3-2012") 'Minute函數可返回表示小時的分鍾數的數字 MsgBox Minute(Now) 'Month函數可返回表示年的月份的數字 MsgBox Month(Now) 'Second函數可返回表示分鍾的秒數的數字 MsgBox Second(Now) 'Time函數可返回當前的系統時間 MsgBox Time 'Timer函數可返回午夜12時以后已經過去的秒數 MsgBox Timer 'TimeSerial函數可把時、分、秒合並成為時間 MsgBox TimeSerial(3,45,50) 'TimeValue函數可返回包含時間的日期子類型的變量 MsgBox TimeValue(Now) 'Weekday函數可返回表示一周的天數的數字,介於1和7之間。 MsgBox Weekday(Date,0) 'WeekdayName函數可返回一周中指定一天的星期名 MsgBox WeekdayName(3) MsgBox WeekdayName(Weekday(Date,1)) 'Year函數可返回表示年份的一個數字 MsgBox Year(date) '*********************************Conversion函數******************************* 'Asc函數可把字符串中的第一個字母轉換為對應的ANSI代碼,並返回結果 MsgBox Asc("A") MsgBox Asc("a") 'CBool函數可把表達式轉換為布爾類型 Dim a a=5 MsgBox CBool(a) 'CByte函數可把表達式轉換為字節類型 Dim b b=134.12345 MsgBox CByte(b) 'CCur函數可把表達式轉換為貨幣類型 Dim c c=12345.12345656 MsgBox CCur(c) 'CDbl函數可把表達式轉換為雙精度類型 Dim e e=123.12454656577435235436 MsgBox CDbl(e) 'Chr函數可把指定的ANSI字符代碼轉換為字符 MsgBox Chr(65) 'CInt函數可把表達式轉換為整數類型 MsgBox CInt(12.3435) 'CLng函數可把表達式轉換為長整形類型 MsgBox CLng(12.676) MsgBox CLng(12.243) 'CSng函數可把表達式轉換為單精度類型 MsgBox CSng(122.355465) MsgBox CSng(21.23222) 'CStr函數可把表達式轉換為字符串類型 MsgBox CStr(false) MsgBox CStr(Date) MsgBox CStr(Empty) MsgBox CStr(Error) '*********************************Format函數******************************* 'FormatCurrency函數可返回作為貨幣值被格式化的表達式,使用系統控制面板中定義的貨幣符號 MsgBox FormatCurrency(21.23456) 'FormatNumber函數可返回作為數字被格式化的表達式 MsgBox FormatNumber(12.23456775,3) 'FormatPercent函數可返回作為百分數被格式化的表達式 MsgBox FormatPercent(3/7,3) '*********************************Math函數******************************* 'Abs函數可返回指定的數字的絕對值 MsgBox Abs(-9) 'Atn函數可返回指定數字的正切 MsgBox Atn(9) 'Cos函數可返回指定數字的余弦 MsgBox Cos(9) 'Exp函數可e的冪次方 MsgBox Exp(2.3) 'Fix函數可返回指定數字的整數部分 MsgBox Fix(-6.325) MsgBox Int(-6.325) 'Log函數可返回指定數據的自然對數 MsgBox Log(10) 'Rnd函數可返回一個隨機數,數字總是小於1但大於或等於0. MsgBox Rnd 'Sgn函數可返回指定數字的符號的整數 MsgBox Sgn(15) MsgBox Sgn(-5) MsgBox Sgn(0) 'Sin函數可返回指定數字的正弦 MsgBox Sin(9) 'Sqr函數可返回一個數的平方根 MsgBox Sqr(9) 'Tan函數可返回指定數字的正切 MsgBox Tan(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 MsgBox IsArray(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) MsgBox Join(b,",") 'LBound函數可返回指示數組維數的最小下標。(始終為0) 'UBound函數可返回指示數組維數的最大下標。 Dim a(5) MsgBox LBound(a) MsgBox UBound(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 MsgBox Len(pos1) MsgBox Len(pos2) MsgBox Len(pos3) 'Mid函數可從字符串中返回指定數目的字符 Dim txt txt="This is a beautiful day!" MsgBox Mid(txt,1,5) MsgBox Mid(txt,2,5) MsgBox Mid(txt,1) MsgBox Mid(txt,5) 'Replace函數可使用一個字符串替換另一個字符串指定的次數 Dim txt txt="This is a beautiful day!" MsgBox Replace(txt,"is","AA",4,2) 'Space函數可返回一個由指定數目的空格組成的字符串 Dim txt txt=Space(20) MsgBox txt MsgBox Len(txt) 'StrComp函數可比較兩個字符串,並返回表示比較結果的一個值 Dim txt1,txt2 txt1="hello" txt2="hell" MsgBox StrComp(txt1,txt2) 'String函數可返回包含指定長度的重復字符的一個字符串 MsgBox String(10,"*") 'StrReverse函數可反轉一個字符串 Dim txt txt="This is a beautiful day!" MsgBox StrReverse(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 MsgBox IsNumeric(x) x=Empty MsgBox IsNumeric(x) x="1 h" MsgBox IsNumeric(x)