怎么用vb自定义函数实现Mid函数的功能。
vb的代码已经遗忘得差不多了,直接求教了。

解决方案 »

  1.   

    Mid函数是从字符串中截取一部分得到新的字符串。自己写可以用Left Right两个函数模拟,或者转换成字符数组再处理。
      

  2.   

    mid 还可以用来修改字符串的值。
    msdn:
    MyString = "The dog jumps"   ' Initialize string.
    Mid(MyString, 5, 3) = "fox"   ' MyString = "The fox jumps".
      

  3.   

    Private Function MyMid(ByVal str As String, ByVal start As Long, Optional ByVal length As Variant) As Variant
        Dim sLeft, sRight As String
        
        If IsMissing(length) Then
            MyMid = Right(str, Len(str) - Len(Left(str, start - 1)))
        Else
            sLeft = Left(str, start - 1)
            sRight = Right(str, Len(str) - Len(sLeft) - length)
            
            str = Replace(str, sLeft, " ")
            str = LTrim(str)
            str = Replace(str, " ", sLeft)
            
            str = Replace(str, sRight, " ")
            str = RTrim(str)
            str = Replace(str, " ", sRight)
            
            MyMid = str
        End If
    End Function
      

  4.   


    Option ExplicitPrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)Sub main()
        Dim s As String
        
        s = "我爱你中国"
        Debug.Print MyMid(s, 4, 2)
    End SubPublic Function MyMid(ByRef strSource As String, ByVal nStart As Long, ByVal nLength As Long) As String
        Dim strBuffer As String
        
        If nStart <= 0 Or Len(strSource) - nStart < 0 Then Exit Function
        If nLength > Len(strSource) - nStart + 1 Then nLength = Len(strSource) - nStart + 1
        
        strBuffer = String(nLength, vbNullChar)
        CopyMemory ByVal StrPtr(strBuffer), ByVal StrPtr(strSource) + (nStart - 1) * 2, nLength * 2
        MyMid = strBuffer
    End Function
      

  5.   

    楼上的代码很好,效率非常高,提一个改进的小建议,不要使用strBuffer这个中间变量了,直接使用MyMid 就行了,这样能避免一次字符串的分配和释放以及复制,效率就更高了。
      

  6.   

    Public Function vMid(ByVal s As String, ByVal l1 As Long, ByVal l2 As Long) As String
    vMid = Left(Right(s, Len(s) - l1 + 1), l2 - l1 + 1)
    End Function
      

  7.   

    7楼的代码看似简短,效率却不高,对left和right的调用要两次分配和释放字符串
      

  8.   

    修改字符串就用3楼说的那个mid语句啊对了,不知楼主为啥不用VB自带的Mid函数,而要自定义一个呢?
      

  9.   

    呵呵 既然是自定义 那我也来个简单的
    Function midX(ByVal s As String, ByVal p As Integer, ByVal l As Integer) As String
    Dim bs() As Byte
    Dim i As Integer
    s = StrConv(s, vbFromUnicode)
    ReDim bs(Len(s) - 1)
    bs() = s
    If p < 1 Then p = 1
    If p > UBound(bs) + 1 Then p = UBound(bs) + 1
    If l < 1 Then l = 1
    If l > UBound(bs) - p + 1 Then l = UBound(bs) - p + 2
    For i = p To l + p - 1
    midX = midX & Chr(bs(i - 1))
    Next
    End Function
      

  10.   

    真的佩服江南大侠的代码,呵呵!对API函数的运用已经炉火纯青了学习了!
      

  11.   

    之所以重写是因为一段宏代码是本机运行很正常,但在windows7 + office2007下提示"can't find project or library", 其中Mid找不到,所以尝试使用自定义方式。可能问题不在于Mid,这些基本的函数为什么不支持呢, 那位大侠遇到过.google中
      

  12.   

    windows7 + office2007下?你说的是VBA环境上吗,这我就不清楚了
      

  13.   

    看看引用了什么,有没有missing的。
    不一定是mid。
      

  14.   

    原因是代码里有变量或函数与VBA函数重名,可以VBA.Mid、VBA.Left形式调用。