添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Dim str As String str = "hello world" Debug.Print Replace(str, "o", "*", , , vbTextCompare) ' 输出hell* w*rld End Sub

9、Str(expression)函数

功能:在VBA中,Str函数用于将一个数字转换为表示该数字的字符串。
语法:Str(expression)
参数:
expression:必需,要转换为字符串的数字或包含数字的表达式。可以是任何有效的数值表达式,包括变量、常量、字段或属性。
返回值:返回表示数字的字符串。

Sub StrExample()
    Dim num As Double
    num = 3.14159
    Dim str As String
    str = Str(num)
    Debug.Print str    ' 输出3.14159
End Sub

10、StrComp(string1, string2[, compare])函数

功能:用于比较两个字符串并返回比较结果。
用法:StrComp(string1, string2[, compare])
参数:
string1: 必需,要进行比较的第一个字符串。
string2: 必需,要进行比较的第二个字符串。
compare: 可选,比较模式。可以是vbBinaryCompare(区分大小写)或vbTextCompare(不区分大小写),默认为vbBinaryCompare。
返回值:
如果string1等于string2,则返回0;
如果string1在string2之前,则返回-1;
如果string1在string2之后,则返回1。

Sub StrCompExample()
    Debug.Print StrComp("abc", "ABC") ' 输出1
    Debug.Print StrComp("abc", "ABC", vbTextCompare) ' 输出0
End Sub

11、Chr(charcode)函数

功能:用于返回指定字符代码的字符。
用法:Chr(charcode)
参数:
charcode: 必需,要返回的字符代码。

Sub ChrExample()
    Debug.Print Chr(65) ' 输出"A"
End Sub

12、Asc(string)函数

功能:用于返回字符串中第一个字符的 ASCII 码值。
用法:Asc(string)
参数:string: 必需,要返回 ASCII 码值的字符串。

Sub AscExample()
    Debug.Print Asc("A") ' 输出65
End Sub

13、StrReverse(string)函数

功能:用于反转字符串中的字符顺序。
用法:StrReverse(string)
参数:string: 必需,要进行反转的字符串。

Sub StrReverseExample()
    Debug.Print StrReverse("hello") ' 输出"olleh"
End Sub

14 、InStr([start, ]string1, string2[, compare])函数

  • 功能:在VBA中,InStr函数用于在一个字符串中搜索一个子字符串,并返回子字符串在字符串中第一次出现的位置。
  • 语法:InStr([start, ]string1, string2[, compare])
  • 参数:
  • start:可选,表示开始搜索的位置。如果省略,则从字符串的第一个字符开始搜索。如果为负数,则从字符串的末尾开始倒计算,-1 表示从字符串的最后一个字符开始搜索。
  • string1:必需,要在其中搜索的字符串。
  • string2:必需,要搜索的子字符串。
  • compare:可选,表示指定比较类型的常数。默认值为vbBinaryCompare。
  • 返回值:如果找到子字符串,则返回子字符串在字符串中第一次出现的字符位置。如果未找到子字符串,则返回 0。
Sub InStrExample()
    Dim position As Integer
    position = InStr(1, "Hello, world!", "llo")
    Debug.Print position    ' 输出3
End Sub

15、InStrRev(string1, string2[, start[, compare]])函数

功能:用于在字符串中查找一个子字符串,并返回其从后往前的位置。
用法:InStrRev(string1, string2[, start[, compare]])
参数:
string1: 必需,要在其中搜索的字符串。
string2: 必需,要查找的子字符串。
start: 可选,用于指定开始搜索的位置。默认为字符串的最后一个字符。
compare: 可选,指定比较模式。可以是vbBinaryCompare(区分大小写)或vbTextCompare(不区分大小写),默认为vbBinaryCompare。

Sub InStrRevExample()
    Dim str As String
    str = "hello world"
    Debug.Print InStrRev(str, "lo") ' 输出4
    Debug.Print InStrRev(str, "LO", , vbTextCompare) ' 输出4
End Sub

在上面的示例中,从字符串的第 1 个位置开始搜索的,并找到字符串”llo”,返回其在字符串中第一次出现的位置,即第 3 个字符。

16、Join(expression[, delimiter])函数

功能:用于将数组中的元素组合成一个字符串。
用法:Join(expression[, delimiter])
参数:
expression: 必需,要组合的数组。
delimiter: 可选,用于指定分隔符的字符或字符串。如果省略该参数,则使用空格作为分隔符。

Sub JoinExample()
    Dim arr(2) As String
    arr(0) = "apple"
    arr(1) = "banana"
    arr(2) = "orange"
    Debug.Print Join(arr, ",") ' 输出apple,banana,orange
End Sub

17、Split(expression, [delimiter], [count], [compare])函数

功能:在VBA中,Split函数用于将一个字符串拆分成一个数组,每个元素都是字符串中的一个子字符串。
语法:Split(expression, [delimiter], [count], [compare])
参数:
expression:必需,要拆分为数组的字符串。
delimiter:可选,指定拆分字符串所用的字符。如果省略,则使用空格字符作为分隔符。
count:可选,指定返回数组的最大维数。如果省略,则返回一维数组。
compare:可选,指定用于比较字符值的方法。默认值为vbBinaryCompare。
返回值:返回一个包含拆分后的字符串的数组。

Sub SplitExample()
    Dim str As String
    str = "apple,orange,grape"
    Dim arr As Variant
    arr = Split(str, ",")
    Debug.Print arr(0)    ' 输出apple
    Debug.Print arr(2)    ' 输出grape
End Sub

在上面的示例中,字符串变量str包含三个水果名,使用逗号作为分隔符进行拆分,并将结果存储在变量arr中。可以使用数组下标访问每个元素。

18、Format(expression[, format[, firstdayofweek[, firstweekofyear]]])函数

功能:用于将表达式格式化为指定格式的字符串。
用法:Format(expression[, format[, firstdayofweek[, firstweekofyear]]])
参数:
expression: 必需,要进行格式化的表达式。
format: 可选,指定格式字符串。
firstdayofweek: 可选,指定一周的第一天。默认为当前系统设置的值(0-7,其中0代表周日,1代表周一,以此类推)。
firstweekofyear: 可选,指定一年的第一周。默认为当前系统设置的值(0-2,其中0代表一年的第一个星期至少包含四天,1代表一年的第一个星期始终包含1月1日,2表示一年的第一个星期始终包含本年的第一个工作日)。

Sub FormatExample()
    Dim now As Date
    now = Now()
    Debug.Print Format(now, "yyyy-mm-dd hh:mm:ss") ' 输出2022-12-24 19:39:22
End Sub

四、VBA中的条件判断函数

1、If(condition, truepart, falsepart)函数

功能:用于根据条件判断返回两个值中的一个。
用法:If(condition, truepart, falsepart)
参数:
condition: 必需,要判断的条件表达式。
truepart: 必需,如果条件为True,返回的值。
falsepart: 必需,如果条件为False,返回的值。

Sub IfExample()
    Dim x As Integer
    x = 5
    Debug.Print If(x > 3, "x大于3", "x小于等于3")
End Sub

这个代码会输出:“x大于3”。

2、Switch(expr-1, value-1[, expr-2, value-2]…, [expr-n, value-n][, default])函数

功能:用于根据多个条件进行选择。
用法:Switch(expr-1, value-1[, expr-2, value-2]…, [expr-n, value-n][, default])
参数:
expr-1: 必需,用于比较的表达式。
value-1: 必需,如果条件满足,返回的值。
expr-2: 可选,用于比较的表达式。
value-2: 可选,如果条件满足,返回的值。
expr-n: 可选,用于比较的表达式。
value-n: 可选,如果条件满足,返回的值。
default: 可选,如果所有条件都不满足,返回的值。如果省略该参数,则默认返回Null。

Sub SwitchExample()
    Dim x As Integer
    x = 2
    Debug.Print Switch(x = 1, "x等于1", x = 2, "x等于2", x = 3, "x等于3", "x不是1、2、3中的任意一个")
End Sub

这个代码会输出:“x等于2”。

五、VBA中的日期时间函数

1、Now函数

功能:返回当前日期和时间。
用法:Now

Sub NowExample()
    Debug.Print Now    ' 输出当前日期和时间
End Sub

2、Date函数

功能:返回当前日期。
用法:Date

Sub DateExample()
    Debug.Print Date    ' 输出当前日期
End Sub

3 、Time函数

功能:返回当前时间。
用法:Time

Sub TimeExample()
    Debug.Print Time    ' 输出当前时间
End Sub

4、Year(date)函数

功能:返回一个整数,表示指定日期的年份。
用法:Year(date)
参数:date:必需,要返回其年份部分的日期。

Sub YearExample()
    Debug.Print Year(#05/20/2000#) ' 输出 2000
End Sub

5、Month(date)函数

功能:返回一个整数,表示指定日期的月份。
用法:Month(date)
参数:date:必需,要返回其月份部分的日期。

Sub MonthExample()
    Debug.Print Month(#05/20/2000#) ' 输出 5
End Sub

6、Day(date)函数

功能:返回一个整数,表示指定日期的日份。
用法:Day(date)
参数:date:必需,要返回其日份部分的日期。

Sub DayExample()
    Debug.Print Day(#05/20/2000#) ' 输出 20
End Sub

7、Hour(time)函数

功能:返回一个整数,表示指定时间的小时部分。
用法:Hour(time)
参数:time:必需,要返回其小时部分的时间。

Sub HourExample()
    Debug.Print Hour(#4:30:45 PM#) ' 输出 16
End Sub

8 、Minute(time)函数

功能:返回一个整数,表示指定时间的分钟部分。
用法:Minute(time)
参数:time:必需,要返回其分钟部分的时间。

Sub MinuteExample()
    Debug.Print Minute(#4:30:45 PM#) ' 输出 30
End Sub

9、Second(time)函数

功能:返回一个整数,表示指定时间的秒部分。
用法:Second(time)
参数:time:必需,要返回其秒部分的时间。

Sub SecondExample()
    Debug.Print Second(#4:30:45 PM#) ' 输出 45
End Sub

10 、DateSerial(year, month, day)函数

功能:返回一个代表指定年、月、日的 Date 类型的值。
用法:DateSerial(year, month, day)
参数:
year:必需,表示要返回的日期的年份部分,可以是一个 4 位数。
month:必需,表示要返回的日期的月份部分,可以是一个从 1 到 12 的整数。
day:必需,表示要返回的日期的日份部分,可以是一个从 1 到 31 的整数,具体取值范围依赖于指定的月份和年份。

Sub DateSerialExample()
    Debug.Print DateSerial(2022, 12, 31) ' 输出 Dec 31, 2022
End Sub

11、TimeSerial(hour, minute, second)函数

功能:返回一个代表指定时间的 Date 类型的值。
用法:TimeSerial(hour, minute, second)
参数:
hour:必需,表示要返回的时间的小时部分,整数从 0 到 23 范围内。
minute:必需,表示要返回的时间的分钟部分,整数从 0 到 59 范围内。
second:必需,表示要返回的时间的秒部分,整数从 0 到 59 范围内。

Sub TimeSerialExample()
    Debug.Print TimeSerial(16, 30, 45) ' 输出 4:30:45 PM
End Sub

12、DateValue(datestring)函数

功能:返回由日期字符串表示的日期值。
用法:DateValue(datestring)
参数:datestring:必需,表示日期的字符串。

Sub DateValueExample()
    Debug.Print DateValue("2022-12-31") ' 输出 Dec 31, 2022
End Sub

1 3、TimeValue(timestring)函数

功能:返回由时间字符串表示的时间值。
用法:TimeValue(timestring)
参数:timestring:必需,表示时间的字符串。

Sub TimeValueExample()
    Debug.Print TimeValue("16:30:45") ' 输出 4:30:45 PM
End Sub

14、FormatDateTime(date[, format])函数

功能:将指定日期格式化为字符串。
用法:FormatDateTime(date[, format])
参数:
date:必需,需要进行格式化的日期。
format:可选,用于指定要使用的日期时间格式,可以是 vbGeneralDate、vbLongDate、vbShortDate、vbLongTime、vbShortTime 中的一种。默认为 vbGeneralDate。

Sub FormatDateTimeExample()
    Debug.Print FormatDateTime(#05/20/2000#, vbShortDate)  ' 输出 5/20/2000
    Debug.Print FormatDateTime(#05/20/2000 14:30#, vbLongDate) ' 输出 May 20, 2000
End Sub

15、DateAdd(interval, number, date)函数

功能:用于对日期进行加减运算。
用法:DateAdd(interval, number, date)
参数:
interval: 必需,一个字符串表达式,指定要添加的时间间隔。
number: 必需,要添加的日期时间间隔数量。
date: 必需,要进行加减运算的日期。
返回值:Date 类型的值。

Sub DateAddExample()
    Dim date1 As Date, date2 As Date
    date1 = DateValue("2022-01-01")
    date2 = DateAdd("m", 1, date1)
    Debug.Print date2 ' 输出2022-02-01
End Sub

16、DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])函数

功能:用于计算两个日期之间的时间差。
用法:DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])
参数:
interval: 必需,一个字符串表达式,指定要计算的时间间隔。
date1: 必需,开始日期。
date2: 必需,结束日期。
firstdayofweek: 可选,指定一周的第一天。默认为当前系统设置的值(0-7,其中0代表周日,1代表周一,以此类推)。
firstweekofyear: 可选,指定一年的第一周。默认为当前系统设置的值(0-2,其中0代表一年的第一个星期至少包含四天,1代表一年的第一个星期始终包含1月1日,2表示一年的第一个星期始终包含本年的第一个工作日)。
返回值:Long 类型的值,表示两个日期之间的时间差。

Sub DateDiffExample()
    Dim date1 As Date, date2 As Date
    date1 = DateValue("2022-01-01")
    date2 = DateValue("2022-02-01")
    Debug.Print DateDiff("d", date1, date2) ' 输出31
End Sub

17、Weekday(date[, firstdayofweek])函数

功能:返回一个整数,表示指定日期所在的星期中的第几天。
用法:Weekday(date[, firstdayofweek])
参数:
date:必需,要返回其所在星期中的某一天的日期。
firstdayofweek:可选,指定一周的第一天。默认为当前系统设置的值(0-7,其中0代表周日,1代表周一,以此类推)。

Sub WeekdayExample()
    Debug.Print Weekday(#05/20/2000#) ' 输出 7,表示星期六
End Sub

1 8、WeekdayName(weekday[, abbreviate[, firstdayofweek]])函数

功能:返回指定星期所对应的文本值。
用法:WeekdayName(weekday[, abbreviate[, firstdayofweek]])
参数:
weekday:必需,一个整数,表示星期几。
abbreviate:可选,一个布尔值,指示是否返回缩写版的星期名称。如果省略该参数,则返回完整的星期名称。
firstdayofweek:可选,指定一周的第一天。默认为当前系统设置的值(0-7,其中0代表周日,1代表周一,以此类推)。

Sub WeekdayNameExample()
    Debug.Print WeekdayName(1)                      ' 输出 Monday
    Debug.Print WeekdayName(1, True)                ' 输出 Mon
    Debug.Print WeekdayName(1, False, vbSunday)     ' 输出 Sunday
End Sub

19、CDate(date)函数

功能:将指定的表达式转换为日期。
用法:CDate(date)
参数:date:必需,需要转换为日期的表达式。

Sub CDateExample()
    Debug.Print CDate("2022-05-20") ' 输出 5/20/2022
End Sub

20、Timer函数

功能:返回从午夜开始经过的秒数。
用法:Timer

Sub TimerExample()
    Debug.Print Timer    ' 输出从午夜开始经过的秒数
End Sub

好了,花了一下午的时间整理下VBA内置函数,希望对大家有所帮助,总的来说,VBA 内置函数能够提高代码的简洁性、可读性和可维护性,从而提高程序的效率和开发效率,所以需要熟悉掌握。

o郭二爷o 原创或整理--转载请注明: https://www.dszhp.com/vba-function.html

,

作者 分类 不会EXCEL