在循环

时间:2016-08-17 23:09:12

标签: excel vba excel-vba

我有一张excel表,其中一列填充了大约10个完整的标准地址,间隔为空(空)

所有地址格式相同:

123街道名称,郊区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. 独立于列大小(但我知道我需要使用"对于LngRow = 2到Wksht.Range(" D"& Wksht.Rows.Count).End (xlUp).Row"
  3. 忽略空值(如果Len(address_string)需要使用> 0则退出)
  4. 将ubound用于双名郊区。
  5. 我认为最好的第一步是构建循环,然后实现案例验证,然后列计数,最后是ubound。

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

或者,您可以使用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 方法将包含文本的单元格列解析为多个列。在这里,我假设你的地址数据在A栏,而郊区只有一个单词:

filec = open (filename, 'r')
lines = filec.readlines ()

for line in lines:
    words = line.split(',')
    # Your code here