?auto_number('自动001‘)
显示为:自动002
一个文本自动加1的自动编号函数。根据参数字符串的内容自动增加1
Function AutoNum(strNum As String) As String
If Right(strNum, 1) = "0" Then
AutoNum = Left(strNum, Len(strNum) - 1) & "1"
Else
AutoNum = StrReverse(Val(StrReverse(strNum)))
AutoNum = Left(strNum, Len(strNum) - Len(AutoNum)) & Format((AutoNum + 1), String(Len(AutoNum), "0"))
End If
End Function
程序虽然比较短,但用到了好几个内置的字符串函数,下面看看各个函数的含义:
StrReverse:字符串取反
Val:字符串转数值,取字符串左边数字,包括空格
Left:取字符串左边指定个数的子字符串,对应的还有Right,Mid
Len:返回字符串长度
Format:格式化字符串,这里是返回带前导0的数值
String:返回重复指定个数的字符的字符串,类似的有Space函数