分隔长消息的方法是哪种?

时间:2016-12-18 14:31:55

标签: excel-vba vba excel

我在接收下面的长消息时遇到问题

  

40 = 50年1月22日= 0.002.60 = 35

系统使用点作为分隔符,而数值也有十进制值。

所需的输出是

  

40 = 1.22
  50 = 0.002
  60 = 35

我现在使用手动方式格式化邮件。希望有更好的方法来克服这一点。

4 个答案:

答案 0 :(得分:1)

以下是我对答案的看法,该答案将=而不是.分开。这样做可以输入40=1.22.50=0.002.60=35.70=120等输入。 (即=右侧的部分不 包含.,它可以是整数。)

Sub SplitDotEqual()
    Dim s As String
    Dim a() As String
    Dim i As Integer
    Dim d As Integer
    'Read from A1
    s = Range("A1").Value
    'Split on the "="
    a = Split(s & ".", "=") ' include an extra "." to ensure that
                            ' the final field is ended
    For i = 0 To UBound(a) - 1
        'Put the "=" back
        a(i) = a(i) & "="
        'Find the last "." before the next "="
        d = InStrRev(a(i + 1), ".")
        'Append everything prior to the "."
        a(i) = a(i) & Left(a(i + 1), d - 1)
        'Write to A2:Ax
        Cells(i + 2, 1).Value = a(i)
        'Strip off everything prior to the ".",
        'leaving just the stuff prior to the "="
        a(i + 1) = Mid(a(i + 1), d + 1)
    Next
End Sub

答案 1 :(得分:0)

假设您有一个点“。”作为小数位,另一个“。”用于分隔数组中的每个元素。您可以使用下面的代码将Long字符串的所有值读入数组(Nums是数组的名称)。

Option Explicit

Sub Seperate_DecimNumbers()

Dim Nums As Variant
Dim FullStr As String
Dim DotPosition As Integer
Dim i As Integer

' init array size to a large size , will redim it at the end to number of elements found
ReDim Nums(1 To 100)

FullStr = "40=1.22.50=0.002.60=35."

i = 1 ' init array elements counter
Do Until Len(FullStr) = 0
    ' call FindN function , searching for the 2nd "."
    DotPosition = FindN(FullStr, ".", 2)

    ' unable to find 2 "." in the string >> last element in the array
    If DotPosition = 0 Then
        Nums(i) = FullStr
        Exit Do
    Else ' was able to find 2 "." in the string
        Nums(i) = Left(FullStr, DotPosition - 1)
    End If

    i = i + 1
    FullStr = Right(FullStr, Len(FullStr) - DotPosition)
Loop

' redim array back to maximum of numbers found in String
ReDim Preserve Nums(1 To i)   

' place output start location from Range A2 and below (till number of elements in the array)
Range("A1").Offset(1, 0).Resize(UBound(Nums), 1).Value = Application.Transpose(Nums)

End Sub



Function FindN(sInputString As String, sFindWhat As String, N As Integer) As Integer
' this function find the Nth position of a certain character in a string    

Dim J As Integer

Application.Volatile
FindN = 0

For J = 1 To N
    FindN = InStr(FindN + 1, sInputString, sFindWhat)
    If FindN = 0 Then ' unable to find the 2nd "." >> last appearance
        Exit For
    End If
Next

End Function

见下面的结果:

enter image description here

答案 2 :(得分:0)

我们假设每隔一个点都是一个分隔符。此代码将奇数点更改为管道,然后解析管道:

Sub parser()
    Dim FlipFlop As Boolean, dot As String, pipe As String
    Dim s As String, L As Long, i As Long, CH As String

    dot = "."
    pipe = "|"
    s = Range("A1").Value
    L = Len(s)
    FlipFlop = True

    For i = 1 To L
        CH = Mid(s, i, 1)
        If CH = dot Then
            If FlipFlop Then
            Else
                Mid(s, i, 1) = pipe
            End If
            FlipFlop = Not FlipFlop
        End If
    Next i

    msg = s & vbCrLf
    ary = Split(s, pipe)
    For Each a In ary
        msg = msg & vbCrLf & a
    Next a
    MsgBox msg
End Sub

enter image description here

答案 3 :(得分:0)

得到更接近的消息,代码部分有用。

  

8 = TEST.1.2.9 = 248.35 =草49 = MMUIJ.56 = FGTUH.34 = 32998.50 = MMTHUJ.57 = AY / ABCDE.52 = 20161216-07:58:07.11 = 00708991.1 = A-12345 -