提取单元格

时间:2015-08-21 14:34:00

标签: vba excel-vba excel

我有一列地址,以及可能匹配的行:

enter image description here

我希望能够突出显示(以绿色表示)我认为最符合“查找地址”的行。

因此,对于A1,由于“查找地址”中没有状态,所以任何人都猜测哪个是“正确的”。 然而,说查找地址是“123 Pennsylvania Ave,PA”。我希望“查找值”下方的“PA”结果以绿色突出显示。

如您所见,我在G列中找到了每个找到的地址的状态。

我已经能够创建一个遍历每个找到的地址范围的循环,但我很难想到如何进行状态匹配。

这是我到目前为止所拥有的:

Sub check_Location_State_Pick_Most_Likely()
'After you run the XML reader and have your data, run this to highlight the most likely match
Dim lastRow As Integer, locStartRow As Integer, locEndRow As Integer
Dim cel As Range, rng As Range
Dim locLookupState As String, locResultState As String

Dim stateArray() As Variant
stateArray = Array("Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming")

Dim stateAbbreviationArray() As Variant
stateAbbreviationArray = Array("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY")

'If IsInArray("californa", stateArray) Then
'    Debug.Print "California is found in the array"
'Else
'    Debug.Print "Californa is not found in the arraay"
'End If

lastRow = ActiveSheet.UsedRange.Rows.Count

' Since each location in column A start with "Lookup Address", we can use that
Dim nextString As String
nextString = "Lookup Address"

Dim noAddresses As Integer
noAddresses = WorksheetFunction.CountIf(Range(Columns(1), Columns(1)), "Lookup Address*")


For i = 1 To noAddresses
    If i = 1 Then
        locStartRow = 1
    Else
        locStartRow = Columns(1).Find(what:=nextString, after:=Cells(locStartRow, 1)).row
    End If

    locEndRow = Columns(1).Find(what:=nextString, after:=Cells(locStartRow, 1)).row
        If locEndRow = 1 Then locEndRow = Cells(60000, 1).End(xlUp).row  ' Because, after cell 71, there is no Lookup Address, so it loops back to top

    If locEndRow = lastRow Then
        Set rng = Range(Cells(locStartRow + 1, 1), Cells(locEndRow, 1))
    Else
        Set rng = Range(Cells(locStartRow + 1, 1), Cells(locEndRow - 1, 1))
    End If

    '  Do something with the ranges!
    Dim stateRange As Range
    Set stateRange = rng.Offset(0, 6)

    ' Now, loop through Cells(locStartRow,1) and try to see if this word exists in your StateArray.

Next i

End Sub

我在想:对于每个带有“查找地址”的单元格,遍历每个单词并查看它是否存在于我的状态数组中。

我不知道如何遍历单元格/字符串中的每个单词。我知道如何做每一个角色,但我在每个单词上打了一堵墙。我想以某种方式使用Mid()来获取每个单词吗?

当然,如果有关于如何做到这一点的任何其他想法(最终目标是关于哪个地址结果与“查找地址”匹配的智能猜测)。

我希望这是有道理的,谢谢你的任何想法!

谢谢cyboashu!我能够把你的答案付诸实践,这就是我想要的:D

Dim arrComma
    arrComma = Split(Range(Cells(locStartRow, 1), Cells(locStartRow, 1)).Value2, " ")

    Dim lCtr As Long
    Dim arrSpace
    Dim lookupState As String
    For lCtr = LBound(arrComma) To UBound(arrComma)
        arrSpace = Split(arrComma(lCtr), Space(1))


        If Right(arrSpace(0), 1) = "," Then arrSpace(0) = Left(arrSpace(0), Len(arrSpace(0)) - 1)
        Debug.Print "Looking for the word: " & arrSpace(0)
        'If IsInArray(StrConv(CStr(arrSpace(0)), vbProperCase), stateArray) Then
        For Each xstate In stateArray
            If StrComp(CStr(xstate), arrSpace(0), vbTextCompare) = 0 Then
                Debug.Print arrSpace(0) & " is found in the full State Name array"
                lookupState = arrSpace(0)
                Exit For
            End If
        Next xstate

    ' Since it didn't find any "Full Lenght" names, check the abbreviations
    If lookupState = "" Then
        Debug.Print arrSpace(0) & " was not found in full state name array, checking abbreviations..."
            For Each xstate In stateAbbreviationArray
            If StrComp(CStr(xstate), arrSpace(0), vbTextCompare) = 0 Then
                Debug.Print arrSpace(0) & " is found in the Abbreviations array!"
                lookupState = stateArray(Application.Match(arrSpace(0), stateAbbreviationArray, False) - 1)
                Exit For
            End If

        Next xstate
        If lookupState = "" Then Debug.Print arrSpace(0) & " was not found in abbreviations array either."
    End If

    If lookupState <> "" Then Exit For

    Next lCtr

    Debug.Print "The state in the Lookup Address is: " & lookupState

1 个答案:

答案 0 :(得分:3)

如何使用split功能。 首先将地址拆分为&#34;,&#34;分离器。

Dim arrComma

arrComma=Split(range("A2").value2,",") 'Your cell address here

然后循环遍历数组的所有元素,并将每个数组拆分为空格。

    Dim lCtr as Long
    Dim arrSpace
    For lCtr= lbound(arrComma) to ubound(arrComma) 
        arrSpace=Split(arrComma(lctr),space(1))
    Next

然后在结果数组arrSpace中循环遍历每个单词。