VBA - 使用startPOS和endPOS拆分STRING函数

时间:2011-12-04 17:49:44

标签: string excel vba split

我需要Excel VBA中的一个函数,它将使用START_POSITION和END_POSITION拆分字符串。

像:

function splitxy(text as String, start_pos as Integer, end_pos as Integer) as String

我是否需要将字符串转换为数组并使用循环来获取所需的字符,还是有更简单的方法?

3 个答案:

答案 0 :(得分:2)

Function splitxy(text As String, start_pos As Integer, end_pos As Integer) As String
splitxy = Mid(text, start_pos, (end_pos - start_pos) + 1)
End Function

答案 1 :(得分:2)

根据要求评论答案:

Function splitxy(Text As String, StartPos As Integer, _
                                           EndPos As Integer) As String 

  splitxy = Mid(Text, StartPos, EndPos - StartPos + 1)

End Function

答案 2 :(得分:0)

就我而言,所有拥有千位分隔符的数字也都有一个小数点逗号。我能够用这种方式解决它:

Function makeNumber(n As String)
  n = Replace(n, ",", ".")

  If Len(n) - Len(Replace(n, ".", "")) > 1 Then
    Dim dots As Integer
    Dim pos_of_last_dot As Integer
    Dim integral As String
    Dim decimal As String

    pos_of_last_dot = InStrRev(n, ".", -1)
    integral = Left(n, pos_of_last_dot)
    decimal = Mid(n, pos_of_last_dot)
    integral = Replace(integral, ".", "")
    decimal = Replace(decimal, ".", "")
    n = integral & "." & decimal

   End If

   makeNumber = n
 End Function