分割字符串两次

时间:2019-01-09 02:07:47

标签: excel vba split

我正在尝试将400多行包含字母和数字的字符串切成不同的列。原始字符串如下所示:

Chicago today 1.01 1.33 1.90
Dallas today 1.76
San Antonio today 3.43 4.67 8.99 2.34 9.65 10.13

理想地,我的最终代码将产生A列中的城市名称,以及随后的每个浮动对象都位于不同的列中,例如:

 A            B    C   D    E    F    G
Chicago     1.01 1.33 1.90
Dallas      1.76
San Antonio 3.43 4.67 8.99 2.34 9.65 10.13

我使用“今天”一词作为分隔符,因为它对最终产品根本不重要,而且我能够从数字中拆分城市名称,但是现在所有数字都存在于B列中,而不是我尝试使用两个定界符:“ today”和“”,但是两个单词的城市名称也被拆分了。

Sub SplitName ( )

Dim Cpty  As String
Dim i As Integer
Dim Rate As Variant

Cpty = ActiveCell.Value

Rate = Split (City, “today”)

For i = 0 To UBound(Rate)
    Cels(1, i+1).Value = Rate (i)


Next i


End Sub

感谢您的帮助,谢谢!

5 个答案:

答案 0 :(得分:1)

我也可以玩吗? :)

我觉得这可能是最快的方法?

逻辑

  1. 在第1列中,将“今天”替换为“ |”
  2. 使用“ |”向第1列上的文本发送到列作为分隔符
  3. 在第2列中,将“”替换为“ |”
  4. 在第2列上使用“ |”文本到列作为分隔符

代码

Sub Sample()
    Dim ws As Worksheet

    Set ws = Sheet1

    With ws
        .Columns(1).Replace What:=" today ", Replacement:="|", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

        .Columns(1).TextToColumns Destination:=.Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :="|", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True

        .Columns(2).Replace What:=" ", Replacement:="|", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

        .Columns(2).TextToColumns Destination:=.Range("B1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :="|", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, _
        1), Array(6, 1), Array(7, 1)), TrailingMinusNumbers:=True
    End With
End Sub

屏幕截图

enter image description here

答案 1 :(得分:0)

拆分为“ today”,然后在空间上拆分第二个(从零开始的一维数组中为1个),并确保您获取的是双精度字符,而不是仅看起来像双精度字符的文本。

Chicago today 1.01 1.33 1.90
Dallas today 1.76
San Antonio today 3.43 4.67 8.99 2.34 9.65 10.13

for i=1 to lastrow
    arr1 = split(rasnge("A" & i), " today ")
    arr2 = split(arr1(1), " ")
    for j = lbound(arr2) to ubound(arr2)
        debug.print cdbl(arr2(j))
    next j
next i

答案 2 :(得分:0)

以下内容

示例

Option Explicit
Sub SplitName()
    Dim City As String
        City = ActiveCell.Value

    Dim Rate() As String
        Rate() = Split(City)

    Dim i As Long
    For i = LBound(Rate) To UBound(Rate)
      Debug.Print Rate(i) ' Print to Immediate window
      Cells(1, i + 1).Value = Rate(i)
    Next i

End Sub

答案 3 :(得分:0)

两次拆分

将一个单元格的值分割为一个列两次,并将结果复制到计算出的列数中。

快速版本

Sub SplitName()

    ' Source
    Const cSheet1 As Variant = "Sheet1"   ' Source Sheet Name/Index
    Const cCol As Variant = "A"           ' Source Column Letter/Number
    Const cFirst As Integer = 1           ' Source First Row
    Const cSplit1 As String = "today"     ' First Split String
    Const cSplit2 As String = " "         ' Second Split String
    ' Target
    Const cSheet2 As Variant = "Sheet1"   ' Target Sheet Name/Index
    Const cFirstCell As String = "B1"     ' Target Range First Cell

    Dim vntS As Variant   ' Source Array
    Dim vnt1 As Variant   ' First Split Array
    Dim vnt2 As Variant   ' Second Split Array
    Dim vntT As Variant   ' Target Array
    Dim lastR As Long     ' Source Last Row
    Dim i As Long         ' Arrays Row Counter
    Dim j As Integer      ' Target Array Column Counter

    ' Paste Source Range into Source Array.
    With Worksheets(cSheet1)
        lastR = .Cells(.Rows.Count, cCol).End(xlUp).Row
        vntS = .Range(.Cells(cFirst, cCol), .Cells(lastR, cCol))
    End With

    ' Calculate number of columns in Target Array.
    For i = 1 To UBound(vntS)
        vnt1 = Split(vntS(i, 1), cSplit1)
        vnt2 = Split(Trim(vnt1(1)), cSplit2)
        If j < UBound(vnt2) Then
            j = UBound(vnt2)
        End If
    Next
    ' Increase the number by one because the first column will be the first
    ' string from First Split Array, and by another one because the
    ' Split Arrays are 0-based.
    j = j + 2

    ' Write Source Array to Target Array.
    ReDim vntT(1 To UBound(vntS), 1 To j)
    For i = 1 To UBound(vntS)
        vnt1 = Split(vntS(i, 1), cSplit1)
        vnt2 = Split(Trim(vnt1(1)), cSplit2)
        vntT(i, 1) = Trim(vnt1(0))
        For j = 0 To UBound(vnt2)
            vntT(i, j + 2) = vnt2(j)
        Next
    Next

    ' Paste Target Array into Target Range calculated from Target First Cell.
    With Worksheets(cSheet2).Range(cFirstCell)
        .Resize(UBound(vntT), UBound(vntT, 2)) = vntT
    End With

End Sub

只需更新给定的代码

Sub SplitName()

    Dim Cpty  As String
    Dim i As Integer
    Dim Rate As Variant
    Dim Rate2 As Variant

    Cpty = ActiveCell.Value

    Rate = Split(Cpty, "today")
    Rate2 = Split(Trim(Rate(1))) ' ***

    ' Write City
    Cells(1, 2).Value = Trim(Rate(0))

    ' Write Numbers
    For i = 0 To UBound(Rate2)
        Cells(1, i + 3).Value = Rate2(i)
    Next i

End Sub

***第二个分隔定界符已被省略,因为默认值为“”。

答案 4 :(得分:0)

还有时间玩吗?

第一步:今天在字符串中找到

第二步:用特殊的(未使用的)定界符替换所有脚趾间隙

第三步:在该特殊分隔符上进行划分

Function SplitCities(ByVal sRow As String) As String()
    Dim today_place As Long
    'Step 1: Look for "today"
    today_place = InStr(sRow, "today")
    'Step 2: Replace the space after today by "|".
    'Since Replace truncate the start of the string, I concatenate it using Left.
    'Here I have removed "today" from the result, but you can use change the values to keep it (use -1 and 0 as offsets)
    sRow = Left$(sRow, today_place - 2) & Replace(sRow, " ", "|", today_place + 5), "|"
    'Step 3: Split the result
    SplitCities = Split(sRow, "|")
End Function

在代码中使用SplitCities而不是Split。