Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > Access数据库-模块/函数/VBA

【Access自定义函数】字符串中数字相关的几个自定义函数

时 间:2013-02-06 05:52:28
作 者:网行者   ID:12122  城市:江门
摘 要:字符串中数字相关的几个函数
正 文:

Public Function IsNumbersOnly(chkStr As String) As Boolean
'检测字符串是否全部由数字组成
   Dim i As Long
   Const AllNumbers = "0123456789"

   IsNumbersOnly = True
   For i = 1 To Len(chkStr)
       If InStr(AllNumbers, Mid(chkStr, i, 1)) = 0 Then
           IsNumbersOnly = False
           Exit Function
       End If
   Next i
End Function

 

Public Function NumberPos(chkStr As String) As Long
'检测字符串中第一个数字的位置
'函数值为0时,表示字符串中不包含数字
   Dim i As Long   
   For i = 1 To Len(chkStr)
       If Mid(chkStr, i, 1) Like "[0-9]" Then
           NumberPos = i
           Exit Function
       End If
   Next i  
End Function


Public Function NoNumbers(chkStr As String) As Boolean
' 检测字符串中是否不含数字
   Dim i As Long

   NoNumbers = True   
   For i = 1 To Len(chkStr)
       If Mid(chkStr, i, 1) Like "[0-9]" Then
           NoNumbers = False
           Exit Function
       End If
   Next i  
End Function

 

Public Function NumberGet(chkStr As String) As String
'从字符串中提取数字
    Dim i As Integer
    For i = 1 To Len(chkStr)
        If Mid(chkStr, i, 1) Like "[0-9]" Then
            NumberGet = NumberGet & Mid(chkStr, i, 1)
        End If
    Next i
    NumberGet = NumberGet
End Function


Public Function CutStr(chkStr As String) As String
'截取字符串中第一个数字前的字符
   Dim i As Long  
   For i = 1 To Len(chkStr)
       If Mid(chkStr, i, 1) Like "[0-9]" Then
           CutStr = Left(chkStr, i - 1)
           Exit Function
       End If
   Next i   
   CutStr = chkStr  
End Function

 

Public Function CutN(chkStr As String) As String
'剔除字符串中的数字
    Dim ObjStr As String
    Dim LngStr As Long
    Dim i As Long
   
    For i = 1 To Len(chkStr)
        LngStr = Asc(Mid(chkStr, i, 1))
        If LngStr < 48 or LngStr > 57 Then
            CutN = CutN + Mid(chkStr, i, 1)
        End If
    Next i

End Function

 

Public Function SortNumber_1(MyString As String) As String
'提取不重复数字并从小到大排列
    Dim i As Integer
    Dim Str As String
    For i = 0 To 9
        If InStr(1, MyString, i) > 0 Then
            Str = Str & i
        End If
    Next
    SortNumber_1 = Str
End Function

 

Public Function SortNumber_2(MyString As String) As Double
'提取不重复数字并从大到小排列
    Dim i As Integer
    Dim Str As String
    For i = 9 To 0 Step -1
        If InStr(1, MyString, i) > 0 Then
            Str = Str & i
        End If
    Next
    SortNumber_2 = Str
End Function

 



Access软件网QQ交流群 (群号:54525238)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助