资本组合后拆分细胞

时间:2014-12-08 11:41:30

标签: string excel split

我有一个包含200行的文件,其值如下所示: ANTWERPEN 3 ABDIJ Abdijstraat 71-73 2020 9:00 18:00 9:00 18:00 9:00 18:00 9:00 18:00 9:00 19:00 9:00 19:00我希望将其分成不同的列。

  1. 我希望完全拥有Capitals的部分有1列。在这个特定情况下,那将是: ANTWERPEN 3 ABDIJ

  2. 在其后面的部分的另一列,直到4个数字字符。在这种情况下:Abdijstraat 71-73

  3. 我很高兴行值区分地址,但我不知道如何做到这一点。

    我在第一个数字字符处拆分单元格的情况类似:

    text to columns: split at the first number in the value

    但现在我正在寻找一个双重解决方案,在第一列中第一部分完全在大写,代表城市,在第二列我需要拥有字符串以大写字母开头,后跟非大写字母,在4个字符的数字字符串之前结束。

    如果我能创建一个可以为我做这个的vba或excel代码/公式,我会很高兴,但不幸的是,我不能: - (

    所以我希望有人可以。

    修改

    找到其他一些例程并修改和测试它,帮助我创建了这个:

    Sub doitall()
       Dim cell As Range, j As Integer, i As Integer, x As String
       Dim str As String
       Dim strlen As Integer
       Dim k As Integer
       Dim l As Integer
       Dim y As Integer
    '   Dim v As Integer
    '
    '
    '   For j = 1 To Cells(Rows.Count, 1).End(xlUp).Row
    '      For Each cell In ActiveSheet.Range(Cells(1, 1), Cells(j, 1))
    '         For i = 1 To Len(cell)
    '         x = Mid(cell, i, 1)
    '         If x = ":" Then Exit For
    '      Next i
    '      cell.Offset(0, 1) = Left(cell, i - 8)
    '      Next cell
    '   Next j
    
    'geparkeerd
    '            If l >= 65 And l <= 90 Then
    '        If v > 1 Then
    '        m = v - 1
    '        l = Asc(Mid(Cells(j, 2), m, 1))
    '        Else
    '        l = 0
    '        End If
    
    
    For j = 1 To Cells(Rows.Count, 2).End(xlUp).Row
        For Each cell In ActiveSheet.Range(Cells(1, 2), Cells(j, 2))
            For v = 1 To Len(cell)
                k = Asc(Mid(cell, v, 1))
                If k >= 97 And k <= 122 Then
                    If v < 1 Then
                    Exit For
                    Else: m = v - 1
                    End If
                        l = Asc(Mid(cell, m, 1))
                        If l >= 65 And l <= 90 Then
                        y = Len(cell) - (v - 1)
                        cell.Offset(0, 1) = Mid(cell, m, y + 1)
                    Else
                    End If
                End If
            Next v
        Next cell
    Next j
    
    End Sub
    

    第一部分找到&#34;:&#34;在单元格值中,使用左边的所有字符来自&#34;:&#34;减去8作为其旁边列中单元格的单元格值。

    第二部分必须使用这个&#39; new&#39;用于将城市名称与街道名称分隔开的值。幸运的是,街道名称始终以资本开头,后面跟着非资本。 幸运的是,城市名称完全是大写字母,这样可以更容易地根据资本和非资本分割价值。

    我现在专注于第二部分。

    第二部分的作用是检查每个单元格以及单元格中的每个位置(如果它是非资本的)。如果是,它会检查之前的位置是否为大写。如果是这样,它必须使用大写中的所有字符作为下一栏中单元格中的新值。

    这很有效。 但不是这个价值: BELLE- ILE "Belle-Ile" Shop 22 -Quai des Vennes 1 该值的结果仅为Vennes 1

    但为什么呢? v从1循环到单元格的长度。但从1开始,因此位置1位于单元格值的左侧。从这个例程中,结果应该是Belle-Ile" Shop 22 -Quai des Vennes 1

    有人对此有解释吗? 我现在会手动调整它,但我很想知道为什么它会返回这个值。

    解决方案: v必须从len(cell) to 1 step -1进行检查。在我改变它之后,它几乎完美地工作。 但我仍然不明白为什么。我是如何阅读的,是v开始在最后一个位置测试,朝着单元格值的第一个位置工作。像这样,在我看来,我相信例行公事是行不通的。但它确实如此。关键是要理解为什么v必须是len(cell) to 1 step -1而不是1 to len(cell)

    我希望有人可以向我解释一下。

    (在我了解它之后,我也会尝试使用正则表达式解决方案。)

2 个答案:

答案 0 :(得分:0)

我是regex的新手,但以下是上面给出的输入行。毫无疑问,存在更优雅的解决方案,但这可能会让您朝着正确的方向前进。我发现StackOverflow链接在构建正则表达式模式时非常有用:

How to match "anything up until this sequence of characters" in a regular expression?

Regex to match mixed case words

Regex to match only uppercase "words" with some exceptions

How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops

Function Part1(Myrange As Range) As String

    Dim regEx As New RegExp
    Dim strPattern As String
    Dim strInput As String

    strPattern = ".+?(?=[A-Z][a-z]+)"

    If strPattern <> "" Then
        strInput = Myrange.Value

        With regEx
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = strPattern
        End With

        If regEx.test(strInput) Then
            Set matches = regEx.Execute(strInput)
            For Each Match In matches
                Part1 = Part1 & Match.Value
            Next
        Else
            Part1 = "Not matched"
        End If
    End If

End Function

Function Part2(Myrange As Range) As String

    Dim regEx As New RegExp
    Dim strPattern As String
    Dim strInput As String
    Dim strReplace As String

    strPattern = ".+?(?=[A-Z][a-z]+)"

    If strPattern <> "" Then
        strInput = Myrange.Value
        strReplace = ""

        With regEx
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = strPattern
        End With

        If regEx.test(strInput) Then
            Part2 = regEx.Replace(strInput, strReplace)
            regEx.Pattern = ".+?(?=[0-9]{4})"
            Set matches = regEx.Execute(Part2)
            For Each Match In matches
                Part2 = Match.Value
            Next
        Else
            Part2 = "Not matched"
        End If
    End If

End Function

答案 1 :(得分:0)

这就是我所拥有的以及满足我的需要&#39;:

Sub doitall()
   Dim cell As Range, j As Integer, i As Integer, x As String
   Dim str As String
   Dim strlen As Integer
   Dim k As Integer
   Dim l As Integer
   Dim y As Integer
   Dim v As Integer

   For j = 1 To Cells(Rows.Count, 1).End(xlUp).Row
      For Each cell In ActiveSheet.Range(Cells(1, 1), Cells(j, 1))
         For i = 1 To Len(cell)
         x = Mid(cell, i, 1)
         If x = ":" Then Exit For
      Next i
      cell.Offset(0, 1) = Left(cell, i - 8)
      Next cell
   Next j

For j = 1 To Cells(Rows.Count, 2).End(xlUp).Row
    For Each cell In ActiveSheet.Range(Cells(1, 2), Cells(j, 2))
        For v = Len(cell) To 1 Step -1
            k = Asc(Mid(cell, v, 1))
            If k >= 97 And k <= 122 Then
                If v < 1 Then
                Exit For
                Else: m = v - 1
                End If
                    l = Asc(Mid(cell, m, 1))
                    If l >= 65 And l <= 90 Then
                    y = Len(cell) - (v - 1)
                    cell.Offset(0, 1) = Mid(cell, m, y + 1)
                    cell.Offset(0, 2) = Left(cell, (m - 1))
                Else
                End If
            End If
        Next v
    Next cell
Next j

End Sub

它几乎完美无缺。除了一些在字符串中有一些其他字符但未被此例程覆盖的单元格。 但我相信也可以添加(检查操作空间,双引号等)。