我正在尝试比较同一工作簿中两张纸上的数据。第一页包含一个单独的地址列表,第二页包含一个地址范围列表,其中一列是起始地址范围,第二列是结束地址范围。例如
sheet1:
123 main st
230 main st
456 main st
Sheet2:
100 200 main st
400 500 main st
如何查找单个地址是否在地址范围内?我有以下与街道名称匹配的代码,但是我需要添加该地址范围内的街道编号的条件,否则不匹配。在此示例中,sheet1第1行和第3行是匹配项,而sheet1第2行不是匹配项。
Sub matchcolumns()
Dim I, total, fRow As Integer
Dim found As Range
total = Sheets(1).Range("A" & Rows.Count).End(xlUp).row
For I = 2 To total
answer1 = Worksheets(2).Range("A" & I).Value
Set found = Sheets(1).Columns("H:H").Find(what:=answer1) 'finds a match
If Not found Is Nothing Then
Debug.Print "MATCH"
Else
Debug.Print "NO MATCH"
End If
Next I
End Sub
答案 0 :(得分:0)
遍历Sheet1,并检查它是否存在于Sheet2中。在这种情况下,MATCH或NO MATCH被写入第三列。干杯。
Option Explicit
Public Sub check()
Dim vDataSheet As Worksheet
Dim vDataRow As Long
Dim vRefSheet As Worksheet
Dim vRefRow As Long
Dim vFound As Boolean
Set vDataSheet = Application.ActiveWorkbook.Sheets("Sheet1")
Set vRefSheet = Application.ActiveWorkbook.Sheets("Sheet2")
vDataRow = 1
While vDataSheet.Cells(vDataRow, 1) <> ""
vFound = False
vRefRow = 1
While vRefSheet.Cells(vRefRow, 1) <> "" And Not vFound
If vDataSheet.Cells(vDataRow, 1) >= vRefSheet.Cells(vRefRow, 1) And _
vDataSheet.Cells(vDataRow, 1) <= vRefSheet.Cells(vRefRow, 2) And _
vDataSheet.Cells(vDataRow, 2) = vRefSheet.Cells(vRefRow, 3) Then
vFound = True
End If
vRefRow = vRefRow + 1
Wend
If vFound Then
vDataSheet.Cells(vDataRow, 3) = "MATCH"
Else
vDataSheet.Cells(vDataRow, 3) = "NO MATCH"
End If
vDataRow = vDataRow + 1
Wend
End Sub
Sheet1之前
Sheet2
Sheet1之后
答案 1 :(得分:0)
@Mikku,谢谢我将数据读取为格式不正确的列...而不是单个列。我的错。这是处理单列数据的更新代码。我对数据类型进行了简单化的假设(假设我不知道它们的真正结构,将街道编号作为字符串)……但是可以使用所讨论的数据示例:
Option Explicit
Public Sub check()
Dim vDataSheet As Worksheet
Dim vDataRow As Long
Dim vStreetNumber As String
Dim vStreetName As String
Dim vRefSheet As Worksheet
Dim vRefRow As Long
Dim vFromNumber As String
Dim vToNumber As String
Dim vFirstSpace As Long
Dim vSecondspace As Long
Dim vRefName As String
Dim vFound As Boolean
Set vDataSheet = Application.ActiveWorkbook.Sheets("Sheet1")
Set vRefSheet = Application.ActiveWorkbook.Sheets("Sheet2")
vDataRow = 1
While vDataSheet.Cells(vDataRow, 1) <> ""
vStreetNumber = Left(vDataSheet.Cells(vDataRow, 1), InStr(1, vDataSheet.Cells(vDataRow, 1), " ") - 1)
vStreetName = Right(vDataSheet.Cells(vDataRow, 1), Len(vDataSheet.Cells(vDataRow, 1)) - InStr(1, vDataSheet.Cells(vDataRow, 1), " "))
vFound = False
vRefRow = 1
While vRefSheet.Cells(vRefRow, 1) <> "" And Not vFound
vFirstSpace = InStr(1, vRefSheet.Cells(vRefRow, 1), " ")
vFromNumber = Left(vRefSheet.Cells(vRefRow, 1), vFirstSpace - 1)
vSecondspace = InStr(vFirstSpace + 1, vRefSheet.Cells(vRefRow, 1), " ")
vToNumber = Mid(vRefSheet.Cells(vRefRow, 1), vFirstSpace + 1, vSecondspace - vFirstSpace - 1)
vRefName = Right(vRefSheet.Cells(vRefRow, 1), Len(vRefSheet.Cells(vRefRow, 1)) - vSecondspace)
If vStreetNumber >= vFromNumber And vStreetNumber <= vToNumber And _
vStreetName = vRefName Then
vFound = True
End If
vRefRow = vRefRow + 1
Wend
If vFound Then
vDataSheet.Cells(vDataRow, 2) = "MATCH"
Else
vDataSheet.Cells(vDataRow, 2) = "NO MATCH"
End If
vDataRow = vDataRow + 1
Wend
End Sub
Sheet2
上的参考数据在 Sheet1
上的匹配结果