1

大家好,我正在 Excel 2003 中运行一个宏来将物业地址与其所有者地址相匹配,因此我最终得到了一份缺席所有者的报告。

所以在:

column A                                       column C 
10 Smith DR Smithville                         10 Smith DVE, Smithfield, 49089 Antartica 

这就是一些原始数据的输入方式,但我需要将此记录和所有其他略有不同的记录匹配,因此宏在搜索缺席所有者地址时不会选择它,然后将所选记录填充到 sheet2。用外行的话来说,如果我可以将 A 列中的前 6 个字符与 C 列中的前 6 个字符进行比较,那么我认为它会按照我需要的方式工作。

有谁知道我如何在下面显示的宏中实现这一点

Sub test()
Dim i As Long, lr As Long, r As Long, ws As Worksheet, value As Variant, 
    val As Variant
Dim sval As Integer, lr2 As Long
Application.ScreenUpdating = False
Set ws = Worksheets("Sheet1")
lr = ws.Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lr
    value = Split(Cells(i, 1).value, ", ")
    For val = LBound(value) To UBound(value)
        sval = InStr(1, Cells(i, 3).value, value(val), 1)
        If sval = 0 Then Range("A" & i & ":" & "C" & i).Interior.Color = 65535
    Next
Next
For r = 2 To lr
    lr2 = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
    If Range("A" & r).Interior.Color = 65535 Then
        Rows(r).Copy Destination:=Sheets("Sheet2").Rows(lr2 + 1)
        lr2 = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
    End If
Next r
Sheets("Sheet2").Cells.Interior.ColorIndex = 0
Application.ScreenUpdating = True
MsgBox "Done Macro"
End Sub

希望我以此处所需的正确格式粘贴了代码。因此,任何帮助和指导将不胜感激。

4

1 回答 1

3

You can use the formula LEFT(). This will check the first 6 characters from the cell in column A to the first 6 characters in column C. If there's a match, it will add the value from column A to the next free cell in column A, Sheet2.

Sub First6Characters()

LastRow = Cells(Rows.Count, "A").End(xlUp).Row
LastRowSheet2 = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row

For i = 1 To LastRow
    If Left(Range("A" & i), 6) = Left(Range("C" & i), 6) Then
        Sheets("Sheet2").Range("A" & LastRowSheet2).Value = Range("A" & i).Value
        LastRowSheet2 = LastRowSheet2 + 1
    End If
Next i

End Sub

Source: http://www.techonthenet.com/excel/formulas/left.php

于 2012-12-05T06:18:47.440 回答