【问题标题】:Running a string splitting function in a loop在循环中运行字符串拆分函数
【发布时间】:2016-08-17 23:09:12
【问题描述】:

我有一个 excel 表,其中一列填充了大约 10 个完整的标准地址,带有间歇性空白(空值)

所有地址格式相同:

123 Street Name, Suburb QLD 4123

我要做的是创建一个自动拆分器,其中在 BUtton7_Click 宏循环通过列并将街道名称与数字、郊区、州代码和邮政编码拆分到单独的列中。感谢这里的贡献者,我得到了一个很好的核心功能,它将作为静态值给出的地址分开。

Sub Button7_Click()
Dim strTest     As String

Dim arr1
Dim arr2

Dim StreetAddress As String
Dim Postcode As String
Dim StateCode As String
Dim SubUrb As String


strTest = "62 Norma Rd, Myaree WA 6154"

arr1 = Split(strTest, ",")
arr2 = Split(Trim(arr1(1)), Space(1))

StreetAddress = arr1(0)
Postcode = arr2(2)
StateCode = arr2(1)
SubUrb = arr2(0)

Range("E3").Value = arr1(0)
Range("F3").Value = arr2(0)
Range("G3").Value = arr2(1)
Range("H3").Value = arr2(2)


End Sub

我面临的问题是让它运行......

  1. 循环中
  2. 与列大小无关(但我知道我需要使用类似“For LngRow = 2 To Wksht.Range("D" & Wksht.Rows.Count).End(xlUp).Row”的内容
  3. 忽略 Null 值(需要使用 if Len(address_string) > 0 Then exit)
  4. 使用 ubound 表示双名郊区。

我认为最好的第一步是构建循环,然后实现案例验证,然后是列计数,最后是 ubound。

但是我尝试使用上一个问题中使用的循环函数,但它不起作用,而且我以前从未使用过 ubound,有人可以帮助我吗?

【问题讨论】:

  • 刚刚为你点赞

标签: vba excel


【解决方案1】:
Sub Button7_Click()
    Dim strTest     As String

    Dim arr1
    Dim arr2

    Dim StreetAddress As String
    Dim Postcode As String
    Dim StateCode As String
    Dim Suburb As String

    Dim LngRow As Long
    Dim i As Integer

    With ActiveSheet
        For LngRow = 2 To .Range("D" & .Rows.Count).End(xlUp).Row
            strTest = .Cells(LngRow, 4).Value

            If Len(Trim(strTest)) > 0 Then

                arr1 = Split(strTest, ",")
                If UBound(arr1) - LBound(arr1) < 1 Then
                    MsgBox "No comma in address on row " & LngRow & " '" & strTest & "'"
                Else
                    arr2 = Split(Trim(arr1(1)), Space(1))
                    If UBound(arr2) - LBound(arr2) < 2 Then
                        MsgBox "Only " & UBound(arr2) - LBound(arr2) & " spaces after the comma in address on row " & LngRow & " '" & strTest & "'"
                    Else

                        StreetAddress = arr1(0)
                        Postcode = arr2(UBound(arr2))
                        StateCode = arr2(UBound(arr2) - 1)
                        Suburb = ""
                        For i = LBound(arr2) To UBound(arr2) - 2
                            Suburb = Suburb & " " & arr2(i)
                        Next
                        .Cells(LngRow, 5).Value = Trim(StreetAddress)
                        .Cells(LngRow, 6).Value = Trim(Suburb)
                        .Cells(LngRow, 7).Value = Trim(StateCode)
                        .Cells(LngRow, 8).Value = Trim(Postcode)
                    End If
                End If
            End If
        Next
    End With
End Sub

【讨论】:

  • 哇,看起来不错!谢谢你。我得到了一个下标超出范围的错误,这似乎是循环内数组的常见错误。是不是因为指定的元素数量不对?该错误具体来自 arr2 = Split(Trim(arr1(1)), Space(1))
  • 您的(非空白)地址之一是否没有逗号?代码假设有一个逗号,因此将原件分成第一部分(arr1(0))和第二部分(arr1(1),并假设第二部分至少有2个空格(创建arr2( 0)、arr2(1)、arr2(2) 和可能的 arr2(3) 等)。
  • 如果出现错误,我会更新答案以显示一个 msgbox。完成。
  • 不是每个地址都有逗号,地址优化器从具有相同标准输出的 Google 提取地址。
  • 它说第 2 行和第 3 行没有逗号,但是有。 12 Smith St, Chatswood NSW 2067 62 Norma Rd, Myaree WA 6154
【解决方案2】:

或者,您可以使用Range.TextToColumns 方法将包含文本的一列单元格解析为几列。在这里,我假设您在 A 列中的地址数据和郊区只有一个词:

Sub AddressSpliter()
Dim LastRow&, iRow&
On Error Resume Next
Application.DisplayAlerts = False
LastRow = Cells(Rows.Count, 1).End(xlUp).Row

For iRow = 2 To LastRow
    Cells(iRow, 1).TextToColumns Destination:=Cells(iRow, 2), DataType:=xlDelimited, Comma:=True
    ResetText2Columns
    Cells(iRow, 3).TextToColumns Destination:=Cells(iRow, 3), DataType:=xlDelimited, Space:=True
    ResetText2Columns
Next
Application.DisplayAlerts = True
End Sub

Sub ResetText2Columns()
On Error Resume Next
Cells(2, 1).TextToColumns Destination:=Cells(2, 1), DataType:=xlDelimited, ConsecutiveDelimiter:=False, _
            Tab:=False, Semicolon:=False, Comma:=False, Space:=False, Other:=False, OtherChar:=False
End Sub

【讨论】:

    猜你喜欢
    • 2020-07-06
    • 2014-09-19
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    相关资源
    最近更新 更多