[英]Compare only some characters in a cell to only some characters in another cell
Hi guys I am running a macro in Excel 2003 to match property addresses to their owners addresses so I end up with a report of absentee owners. 嗨,大家好我在Excel 2003中运行一个宏来匹配属性地址和它们的所有者地址,所以我最终得到了一个缺席所有者的报告。
So in: 所以在:
column A column C
10 Smith DR Smithville 10 Smith DVE, Smithfield, 49089 Antartica
This is how some of the raw data has been input but I need for this record and all the other slightly different records to be a match and therefore not selected by the macro as it searches for absentee owners addresses then populates the selected records to sheet2. 这是一些原始数据的输入方式,但我需要这个记录和所有其他略有不同的记录匹配,因此宏不会选择它,因为它搜索缺席所有者地址然后将所选记录填充到sheet2。 In laymans terms if I could compare say only the first 6 characters in column A to the first 6 characters in column C then I think it would work the way I need it to. 在外行人的术语中,如果我可以比较只说A列中的前6个字符和C列中的前6个字符,那么我认为它将按我需要的方式工作。
Does anyone know how I can achieve this within my macro shown below 有谁知道如何在我的宏中显示如下所示
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
Hopefully I have pasted the code in the correct format required here. 希望我已经以这里所需的正确格式粘贴代码。 So any help and guidance would be much appreciated. 因此,非常感谢任何帮助和指导。
You can use the formula LEFT()
. 您可以使用公式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. 这将检查A列中单元格中的前6个字符到C列中的前6个字符。如果匹配,它会将A列中的值添加到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 资料来源: http : //www.techonthenet.com/excel/formulas/left.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.