VBA基础
自定义提取数据的前缀
2017-04-17 17:16:20

现有这样的数据AD090809,ABC090807,ABCD123,AB231111如何提取前面的非数字的字符,结果应为AD,ABC,ABCD,AB

函数一:

Public Function gStr(ByVal strTblName As String, _

                     ByVal strFldName As String, _

                     OldStr As String) As String

    Dim rs As New ADODB.Recordset

    Dim i As Integer

    Dim tempStr As String

    Dim strSQL As String

    strSQL = "select * from " & strTblName & " where " & strFldName _

           & "='" & OldStr & "'"

    With rs

        .Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockReadOnly

        For i = 1 To Len(.Fields(strFldName))

            tempStr = Mid(.Fields(strFldName), i, 1)

            If IsNumeric(tempStr) Then

                gStr = Left(.Fields(strFldName), i - 1)

                Exit For

            End If

        Next

        .Close

    End With

    Set rs = Nothing

End Function

函数二:

Public Function gStr(OldStr As String) As String

    Dim tempStr As String

    Dim i As Integer

    For i = 1 To Len(OldStr)

        tempStr = Mid(OldStr, i, 1)

        If IsNumeric(tempStr) Then

            gStr = Left(OldStr, i - 1)

            Exit For

        End If

    Next

End Function